How to use List in Python ?

Python List

A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

Use the code below to create & print a list 

mycolors= [“red”“blue”“green”]

You access the list items by referring to the index number, index start from 0, in case you want to access/print the

last item from the list “mycolors” above you can use the code

print( mycolors[2] )

You can even use the Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc. You can print last item in the list using the code print( mycolors[-1] )

You can also specify a range of indexes by specifying where to start and where to end the range.

thislist = [“apple”“banana”“cherry”“orange”“kiwi”“melon”“mango”]

The code above will Return the third, fourth, and fifth item

Note: The search will start at index 2 (included) and end at index 5 (not included).

We can use the code below to change the value of any item in the list :

mycolors= [“red”“blue”“green”]
mycolors[1] = “black”

Check list length

mycolors= [“red”“blue”“green”]

Check if Item Exists

To determine if a specified item is present in a list use the in keyword:

mycolors= [“red”“blue”“green”]
if “blue” in mycolors:
  print(“Yes, ‘blue’ is in the mycolors list”)

Add Items

To add an item to the end of the list, use the append() method:

mycolors= [“red”“blue”“green”]

To add an item at the specified index, use the insert() method

Insert an item as the second position:

mycolors= [“red”“blue”“green”]

Remove Item

There are several methods to remove items from a list

The remove() method removes the specified item:

mycolors= [“red”“blue”“green”]

The pop() method removes the specified index, (or the last item if index is not specified):

mycolors= [“red”“blue”“green”]

The del keyword removes the specified index:

mycolors= [“red”“blue”“green”]
del mycolors[0]

The del keyword can also delete the list completely:

mycolors= [“red”“blue”“green”]
del mycolors

The clear() method empties the list:

mycolors= [“red”“blue”“green”]

Make a copy of a list with the copy() method:

mycolors= [“red”“blue”“green”]
mylist = mycolors.copy()

Make a copy of a list with the list() method:

mycolors= [“red”“blue”“green”]
mylist = list(mycolors)

How to Print all items in the list using loop :

mycolors= [“red”“blue”“green”]
for x in mycolors:

List Methods

Python has a set of built-in methods that you can use on lists.

Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list

You can find more Python Tutorials  on

Comment here

75 + = 83