logo codesdope

List in Python

List is a collection in python. In most languages, it is called array. We often need 'list' in our programs. Imagine you are writing a program to store marks of every student in a class of 50. Taking 50 different variables is not a good option and here comes list in action.

a = []

a is a list here (empty list). [ ] represents a list.

a = [1,2,3,4]
print a
[1, 2, 3, 4]

Here, 'a' is a list of four integers.

print type ([])
<type 'list'>

As you have seen now that type([]) is giving us a list. This means that '[]' is a list as mentioned above.

Index -> Every member in a list is known by its position, starting from 0. E.g.- in the list [3,4,2,5], index will start from 0. So, index of 3 is 0, 4 is 1, 2 is 2 and 5 is 3.
So, here index started from 0 and went up to 3.

element 2 3 15 8 48 13
index 0 1 2 3 4 5
list in python

So, to access any element of a list,
we write name_of_list[index]

To access 2nd element, we write a[1] ( as the 1st element will have index 0 and second will have index 1.). It will give its value at position 1.

This example will make it clear.

a = [4,9,6,2]
print a[0]
print a[1]
print a[2]
print a[3]

We can use different elements of list as different variables.

We can also change the value of an element of a list as:
a = [1,2,3,4,5]
a[1] = 5
print a
print a[1]
[1, 5, 3, 4, 5]

Here, a[1] = 5 will simply make the element at the 1st index of 'a' equal to 5.

Lists are mutable -> It means that we can change the value of any member of the list as we have done above. (a[1] = 5).
It is not necessary in Python for the list to contain only the same type of elements. We can also have:

a = [1,2,"anything",4.0]
print a
[1, 2, "anything", 4.0]

So, the above list contains int, string and float also.

range() function -> It gives us a list in between the two integers given to range. Let's make it clearer by an example.

print range(1,5)
print range(10)
print range(1,11)
print range(0)
print range(1,10,2)
[1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 3, 5, 7, 9]

range(1,10,2) will create a list of numbers but with a step of 2. It means that it will give a list with the numbers in arithmetic progression with a difference of 2 as done in the above example.
We can have any step instead of 2. Try range(1,10,3) yourself.

You can see official python documentation for this function.

See that range(1,5) includes 1 but not 5.
range in python

Another useful function is len(). It gives us the length ( number of element ) of a list.

a = range(1,5)
print a
print len(a)
b = [2,3,23,23]
print len(b)
[1, 2, 3, 4]

As you saw, we used len to show that both 'a' and 'b' contain 4 elements.

Let's use while

To find sum of the elements in a list

a = [3,23,4,3,12,54,65,23,23,45,32,54,23]
sum = 0
i = 0
while (i<len(a)):
	sum = sum+a[i]
print sum

len(a) will give 13. So, 'i' will go up to 12.
sum = sum+a[i] -> Initially, the value of 'sum' is 0. Now going step by step:
i is 0, sum is 0 and a[0] is 3. So, sum = sum + a[i] will be sum = 0+3 i.e. 3. So, sum is 3 now.
Now, i is 1 and sum is 3 and a[1] = 23. So, sum = 3+23 will make sum 26.
And so on. This will go up to i = 12.

You can also take the elements of the list as input from user using
a[i] = input().
Try this, take 10 integers of a list from user and calculate its sum.

Python provides a better way to iterate over the whole list using for, which we will learn later.

Let's slice the list

a = [1,2,3,4,5]
b = a[1:3]
print b
[2, 3]
range in python

a[1:3] - This will return the elements from 1st index to 2nd i.e. [2,3]. Now, try getting output of a[:3], a[3:] and a[:] on your own.

We can also give step to slice. Let's see this example.

a = [1,2,3,4,5]
b = a[::2]
print b
print a[::3]
print a[::-1]
[1, 3, 5]
[1, 4]
[5, 4, 3, 2, 1]

Step of 2 will take every 2nd element starting from the 1st element of the list. Step of 3 will take every 3rd element. Step of -1 will reverse the list.

Let's complete our knowledge of list by learning to delete.
To delete any index positioned element, we use del() function.

a = [1,2,3,4]
print a
[1, 3, 4]

As we saw, it deletes the 1st element of the list.

List inside list

We can also make lists inside list. This is the way to do this:

a = [[1,2,3],[4,5,6],[7,8,9]]
print a[1][1]
print a[0][1]
print a[2][1]
print a[1][2]

a[0][1] -> a[0] is [1,2,3] and in this list, the element with the index of 1 is 2. So, a[0][1] is 2.

Something more

Let's try this

a = [1,2,3]
b = [4,5,6]
c = a+b
print c
print c
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 12]

a+b : '+' joins two lists.
c.append() append() adds an element in front of the list.

append in list in python

a = [1,2,3]
c = a*2
print c
print 2 in a
[1, 2, 3, 1, 2, 3]

* repeates the list.
in is used to check if an element is present in a list or not.


map is another useful function available in Python. Suppose you have a list of characters and you want to convert all of them to int in one go, then you can use 'map'. Let's see an example of map:

a = ['1','2','3','4']
b = map(int,a)
print b
[1, 2, 3, 4]

You can see that all the elements of the list are ints in the result.


index is a function which gives us the index of an element of a list. It will be clear from the example given below:

a = ['1','2','3','4']
print a.index('2')

As you have seen, a.index('2') will return the index of '2' in the list 'a'.

Programming is a skill best acquired by practice and example rather than from books.
-Alan Turing

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question

Welcome.please sign up.


Welcome.please login.