A List in python is a data type which acts as a container and holds different objects together in an order.
A list is a mutable object, i.e., it can be modified once it’s created. It can be used to store integers or any other data types together in an order. e.g. [1,7,3,8,5], [‘a’,’b’,’c’,’f’], [2,’abcdef’,9,10] etc.
Indexing of a List –
Data in the ith index can be accessed by using listname[i]. Here listname is the name of the list and i is the index which is to be accessed.
The index number of lists starts from zero and goes up to n-1, where n is the number of elements in the list.
Some examples will clear this up-
a=9 #updating value at second index
print(a) # Value gets updated
[2, 3, 4, 5, 6]
[2, 3, 9, 5, 6]
From the above example, we can see that the elements of the list a – 2,3,4,5,6 have indices 0,1,2,3,4 respectively.
Printing a would print the whole list whereas printing a[i] would print the element in the ith index. The use of lists having integers is pretty much same as that of using an array in C/C++. But in C++, we can only store items with same data type. But this problem is not in Lists.
The list is also mutable. This can be seen as a was changed from 4 to 9, and the output confirms it without any errors.
We can use lists for different data types as well.
a=["string1",3,"string2",5,6] # Holds multiple type of objects in a list
In this, the object which is in the Index number 0, gets printed. Hence, the output is string1.
Operations on Python Lists –
Python lists have a special advantage of slicing in which a portion or a section of a list can be simply obtained without any trouble. Taking a look at the following example –
print(a[0:3]) #This prints elements with indices 0,1,2
print(a[:5]) #Similarly, upto indices less than 5, i.e. 0,1,2,3,4
print(a[1:2]) #Only index 1
print(a[2:]) #Starting from index 2, to the length of list
[1, 3, 4]
[1, 3, 4, 5, 6]
[4, 5, 6, 8]
From the above example, you can see that
- if we print a[i:j], the output would be the elements from index i to index j-1.
- for a[i:] the output would be from index i to last element and
- for a[:j], it will be from 0th index to index j-1.
This simple feature of Python lists is called slicing.
Negative Indexing and Slicing –
The indexing can also be done from the last index. If we want to access the elements from the reverse order, we use this method. Similarly, slicing can also be done by specifying negative indices. This can be cleared out from this example –
print(a[-1]) #prints 1st element from the reverse direction
print(a[-4:-2]) #prints the 4th and 3rd element from the last, i.e. indices 2,3
[3, 4, 5, 6, 8]
From the above example, you can see that if we print a[-i], the output would be the i th element from the end of the list. Slicing is just as the same, but just with negative indices.
Deleting Elements –
Deleting elements by index can be done by using the del keyword, e.g.
del a #deletes element in index 2
del a[3:5] #deletes elements with indices 3,4
del a[-1] #deletes last element
[1, 3, 5, 6, 8]
[1, 3, 5]
Input Format –
Now we come to the next question.
How to take a list as an input? Suppose I have a sequence of numbers separated by spaces, which I want to store in a list by taking user input. How to achieve this?
Let’s have an overview of the process. As previously stated, Python takes the user input line by line in string format. So it takes a sequence of numbers which are separated by spaces, then we have to take out the numbers between those spaces and convert them into integers. This process can be done in many ways. The easiest to understand is –
2 4 5 6
[2, 4, 5, 6]
Now let’s just have an explanation of what I did there. There are 3 steps involved here
- Line 1 – The user input 2 4 5 6 is stored in a string format, i.e. “2 4 5 6” in a variable ‘s’.
- Line 2 – The input string “2 4 5 6” is then split by spaces (If you look at the split() function, the parameter passed is a space character). Then this string format is mapped to integer format using the map() function and is stored in a variable b.
- Line 3 – The result b is finally converted into a list and stored in the variable a.
The 3 lines of the previous program can be written in just one line. The program for that would be –
2 4 5 6
[2, 4, 5, 6]
Go through the explanation once more if you have any difficulties in understanding.
NOTE : Here I didn’t pass any parameters in the split() function because the default character which it uses to split is the space character. Hence, it is not necessary to pass the space character there.
Built-in Functions frequently used for lists
- cmp(list1, list2)
How to iterate on a List
Iteration is done by using loops. Here is an example for an array ‘arr’, using for loop –
for i in range(0,len(arr)):
2 4 5 6
Some other methods on lists are used to add an element, add another list, remove an element, sort the list, check the existence of an element and other basic functionalities. Check out official documentation for complete list of methods and functionalities.
That’s all for this tutorial. If you have any doubts, please post them in the comments section. Any ideas/suggestions are welcome.
Do not forget to share, like and spread the knowledge. 🙂