List Operations in Python – Free Python Tutorials

Now, as we know Lists are very important in Python programming, so we need to know about the operations we can perform using Lists. We will study different list operations in python.

There are a number of operations we can perform using Lists :

  • Checking availability of an item in a List
  • Reassignment of an index with a new value
  • Addition of Lists
  • Multiplication of Lists

We will discuss the above topics one by one.


Checking Availability Of An Item In A List

To check the availability of an element in a List, two operators can be used. The operators are in and not


in operator in python

If we check the availability of an item in a List using in operator, then the operator will return True if the item is there in the List and the operator will return False if the item is not there in the List.

As example :

ls = [1,2,3,4,5]

Let’s assume we have the above List ls with 5 integer values starting from 1 to 5. Now we want to check if 2 and 6 are there in the List or not. For this purpose we will use in operator as below :

print(2 in ls)
print(6 in ls)

Now, as 2 is present in the List ls but 6 doesn’t, so we will get the output as below :

>>>True
False

not operator in Python

If we check the availability of an item in a List using not operator, then the operator will return False if the item is there in the List and the operator will return True if the item is not there in the List.

As example :

ls = ["Rajesh","Sinha","Padhle","Internship"]

Let’s assume we have the above List ls with 4 String objects, “Rajesh”, “Sinha”, “Padhle”, and “Internship”. Now we want to check the availability of “Rajesh” and “.com” in the List. For this purpose we will use not operator as below :

print(not "Rajesh" in ls)
print(not ".com" in ls)

Now, as “Rajesh” is present in the List, but “.com” doesn’t so we will get the output as :

>>>False
True

Reassignment of an index with a new value

The item at a certain index in a list can be reassigned.

Let’s explain this point with an example :

Assume we have the following List –>

ls = [1,2,3,"Rajesh",5,6]

In the List, there are 5 numerical values in the indexes 0, 1, 2, 4, and 5. But at index 3 there is a String object present. So this is a heterogeneous List. Now we want to assign the value 4 at index 3, so that the List becomes homogeneous. So we will reassign the index 3 of the List ls with the value 4 as :

ls[3] = 4

Nothing much we have to write. We just need to provide the index at which we need to reassign value and on the other side of the assignment operator, the value will be there.

After executing the following command the List ls will look like :

ls = [1,2,3,4,5]

We can reassign any index of a List as many time we want with any type of value. There is no restriction.


Addition of Lists in Python

Lists can be added like any String objects, the process is like the String operation concatenation.

Let’s assume we have the following two Lists :

ls1 = [1,2,3,4]

ls2 = [9,8,7,6]

Now, it is possible to add these two Lists.

To add those two Lists we will use the ‘+’ operator as below :

print(ls1+ls2)

As output we will get the following result :

>>>[1, 2, 3, 4, 9, 8, 7, 6]

As this addition process is same as String operation concatenation so we are getting the above output. If the process would have been same as the numeric addition operation then we would have got the below result :

>>>[10, 10, 10, 10]

Multiplication of Lists in Python

In case of multiplication of a List, the elements of a List are not multiplied with the multiplier, rather the set of the elements are repeatedly added inside the List.

Let’s clear this point with an example :

Assume we have the following List :

ls = [5,6,7]

Now we are multiplying the above List with 3.

print(ls * 3)

As output we will get :

>>>[5, 6, 7, 5, 6, 7]

Note that, the multiplication of a List is not same as multiplication of two numeric values. The items of the List ls (i.e. 5, 6, 7) are added 3 times in the List as we have multiplied the List with 3.

If the process would have been same as the numerical multiplication process, then we would have got the below output :

>>>[15, 18, 21]

But that is not the case.


This was all about List operations.

Leave a Comment

Your email address will not be published. Required fields are marked *