logo codesdope

List in Python


List is a collection in python. It is similar to the array of most languages. 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.

  • Python 2
  • Python 3
a = []
a = []
Output

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

  • Python 2
  • Python 3
a = [1,2,3,4]
print a
a = [1,2,3,4]
print(a)
Output
[1, 2, 3, 4]

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

  • Python 2
  • Python 3
print type([])
print(type([]))
Output
<class 'list'>

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

Index → Every member in a list is known by its position, starting from 0. For example, 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. Thus, the 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 the 2nd element, we write a[1] (as the index of the 1st element will be 0 and that of the second element will be 1). It will give its value at position 1.

This example will make it clear.

  • Python 2
  • Python 3
a = [4,9,6,2]
print a[0]
print a[1]
print a[2]
print a[3]
a = [4,9,6,2]
print(a[0])
print(a[1])
print(a[2])
print(a[3])
Output
4
9
6
2

We can also change the value of an element of a list as:

  • Python 2
  • Python 3
a = [1,2,3,4,5]
a[1] = 5
print a
print a[1]
a = [1,2,3,4,5]
a[1] = 5
print(a)
print(a[1])
Output
[1, 5, 3, 4, 5]
5

Here, a[1] = 5 will simply make the element at the 1st index of 'a' 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 list as shown in the example given below:

  • Python 2
  • Python 3
a = [1,2,"anything",4.0]
print a
a = [1,2,"anything",4.0]
print(a)
Output
[1, 2, "anything", 4.0]

So, the above list contains int, string as well as float also.

range → It gives us a sequence of numbers in between the two integers given to it. We can convert it to a list by using list() as we did with the integers. Let's make it clear by an example.

  • Python 2
  • Python 3
print range(1,5)
print range(10)
print range(1,11)
print range(0)
print range(1,10,2)
print(list(range(1,5)))
print(list(range(10)))
print(list(range(1,11)))
print(list(range(0)))
print(list(range(1,10,2)))
Output
[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 sequence of numbers but with a step of 2. It means that it will give a sequence with the numbers in arithmetic progression with a difference of 2 as done in the above example. We can also have a different step instead of 2. Try range(1,10,3) yourself.

You can see official Python's documentation for this more.

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

len() is an useful function which we use on a list. It gives us the length (number of elements) of a list.

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

As can see from the above example that we have 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

  • Python 2
  • Python 3
a = [3,23,4,3,12,54,65,23,23,45,32,54,23]
s = 0
i = 0
while (i<len(a)):
	s = s+a[i]
	i=i+1
print s
a = [3,23,4,3,12,54,65,23,23,45,32,54,23]
s = 0
i = 0
while (i<len(a)):
	s = s+a[i]
	i=i+1
print(s)
Output
364

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() or a[i] = int(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 loop, which we will learn later.

Slice


  • Python 2
  • Python 3
a = [1,2,3,4,5]
b = a[1:3]
print b
a = [1,2,3,4,5]
b = a[1:3]
print(b)
Output
[2, 3]
range in python

a[1:3] → This will return the elements from the 1st index to the 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 the slice. Let's see this example.

  • Python 2
  • Python 3
a = [1,2,3,4,5]
b = a[::2]
print b
print a[::3]
print a[::-1]
a = [1,2,3,4,5]
b = a[::2]
print(b)
print(a[::3])
print(a[::-1])
Output
[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.

  • Python 2
  • Python 3
a = [1,2,3,4]
del(a[1])
print a
a = [1,2,3,4]
del(a[1])
print(a)
Output
[1, 3, 4]

As we saw, a[1] deleted the element at the 1st index from the list.

2D List (List inside List)


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

  • Python 2
  • Python 3
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 = [[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])
Output
5
2
8
6

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] will give us the element at index of 1 from the list [1,2,3] (a[0]) i.e., 2.

+, append, * and in


Let's try this

  • Python 2
  • Python 3
a = [1,2,3]
b = [4,5,6]
c = a+b
print c
c.append(12)
print c
a = [1,2,3]
b = [4,5,6]
c = a+b
print(c)
c.append(12)
print(c)
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 12]

a+b → '+' joins two lists.

c.append() → append() is a function which adds an element in the front of the list.

append in list in python

  • Python 2
  • Python 3
a = [1,2,3]
a = [1,2,3]
c = a*2
print c
print 2 in a
a = [1,2,3]
c = a*2
print(c)
print(2 in a)
Output
[1, 2, 3, 1, 2, 3]
True

* repeates the list.

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

map


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:

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

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

index


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

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

You can see that a.index('2') returned the index of '2' in the list 'a'.

You can learn more about lists by reading articles in the Further Reading at the end of this chapter.

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
Close

Welcome.please sign up.

Close

Welcome.please login.