Python Lists: Add, Update, Retrieve Items

We know that Python has four collection data types or Arrays. Those are list, tuple, set, and dictionary. In this tutorial, we will explain about lists. A list is a collection that is used for storing data or values. It is ordered and changeable and allows duplicate members.

Lists and tuples are the same in Python. One major difference is lists are changeable but tuples are unchangeable. Lists are written in square brackets. The basic syntax is given below,

mylist = [10, 20, 30, 40]

# We can store various types of data in a list. For example,
mylist = [10, 20.0, 30.5, 'Python']

Access Items with Indexing

By giving the exact index number, we can access items in the list. We have to use the index number in the square bracket. The index number starts from zero. The index can be negative as well. It means beginning from the end. So, -1 refers to the last item. We can access a range of indexes. We have to mention the start index and the last index. The concept is the same as a tuple.

The result will return in a format of a new list. The range index also can be negative. We have to keep in mind that the start index will be included and the last index will be excluded. If we do not mention the start index, then it will start from the first item. Similarly, if we do not mention the last index, then it will go to the end of the list. Examples are given below,

mylist = [10, 30, 50, 70]

print(mylist[1]) #30
print(mylist[-2]) #50
print(mylist[1:3]) #[30, 50]
print(mylist[-4:-1]) #[10, 30, 50]
print(mylist[:2]) # [10, 30]
print(mylist[2:]) # [50, 70]

Change Item List

If we want to change a specific item, then we have to modify that item by using the index number. An example is given below,

mylist = [10, 30, 50, 70, 90]

print(mylist) #[10, 30, 100, 70, 90]

Loop through a List

We can print all the items by using for loop. For example,

mylist = [10, 30, 50, 70, 90]

for i in mylist:

Checking Item Exists or not

By using 'if statement' and the membership operator ‘in’ we can check an item exist in a list or not. An example is given below,

mylist = [10, 30, 50, 70, 90]

if 50 in mylist:

List Length

We can measure the length of a list in Python. We have to use the len() function to compute the length. For example,

mylist = [10, 30, 50, 70, 90]

print(len(mylist)) #5

Add Items in List

We can add as many items as we want into the list. We have to use the append() method to add an item into an existing list. By this method, the new item will be added at the end of the list. We also can use the insert method to add an item. Here we have to give the index number along with the new value. Examples are given below,

mylist = [10, 30, 50, 70, 90]
print(mylist) #[10, 30, 50, 70, 90, 100]

mylist.insert(2, 40)
print(mylist) #[10, 30, 40, 50, 70, 90, 100]

Remove Items from List

To remove an item, we have to use the remove() method and pass the value. We can do the same operation by using the pop() method. Here we have to write the index number, otherwise, it will remove the last item. Del keyword is used to remove the specified index. It will also be used to delete the entire list as well. The clear () method is used to empty the list. Examples are given below

mylist = [10, 30, 50, 70, 90]
print(mylist) #[10, 30, 50, 90]

print(mylist) #[10, 30, 90]

print(mylist) #[10, 30]

print(mylist) #[30]