I have already explained about LIST in Python in my previous article WHAT IS LIST IN PYTHON. In this article, I am going to explain about List Operators.
List operators in Python are operations that can be performed on lists. A list is a collection of elements, and these operations allow you to manipulate the elements within the list in various ways. These operators allow you to perform a variety of tasks with lists, making them a very powerful and flexible data structure in Python.
Let’s discuss list’s operators with examples :
Concatenation Operator :
This operator is used to join two or more list and return a new list. Example
Note : “list1 + list2” and “list2 + list1” will not give the same result.
If you want to use + operator with list, then both the operands (left side and right side of operator) must be of list types, otherwise it will generate an error. For example :
Note : you cannot do the following with concatenate operator (+) and list :
list + number, number + list, list + string, string + list
Outputs and Errors of the above code :
Replication (*) Operator with List
By using * operator with List, you can replicate your list with specified number of times.
Syntax
number*list or list*number
Example
Membership Operator :
There are 2 membership operators :
Comparison Operator
All comparison operators (relation operators >, <, >=, <=, ==, !=) apply to list also. Python compares individual elements of lists in lexicographical order (dictionary order). Example :
Outputs of the above code:
Let’s discuss some interesting points about lists :
- Lists are mutable, i.e. You can change the individual element of a list in place by assignment, like :
- You can also check that a list is empty or not using “not”
Iterating over multiple lists simultaneously
What if you want to access more than one list simultaneously?
For this, you have to use zip() function, which allows you to do that using one line of code. This function takes multiple lists as the arguments and iterates through them at the same time. The iteration stops when the smaller list is exhausted.
- You can also convert any sequence like string, tuple, sets etc into list by using list() method. Example :
Shallow and Deep Copy:
Whenever you want to copy the list, you will go through this concept. Whenever you will assign one list into another, both refer to the same list i.e. changing one changes to another. Even, the ids (address) of both the lists are same. This is refer as SHALLOW COPY. Example :
So, if you are facing the above problem and want that, if you change one list then the changes are not reflected in another, then you will go with DEEP COPY. You can achieve this by two ways i.e. by list() method and by using copy(). Example :
Using list() method :
Using copy() method:
In conclusion, list operators are an essential aspect of working with lists in Python. They provide a way to manipulate and process lists in various ways, allowing you to perform a wide range of tasks with ease. Whether you are concatenating lists, testing for membership, slicing and indexing elements, or using built-in functions to perform calculations, the list operators in Python are designed to make your life easier. With a good understanding of list operators, you will be able to write more efficient and effective code when working with lists in Python.