List Data type

By | May 23, 2018

Till now we have not seen data types which holds multiple values. Let us start with such data types:

List, Tuple, Set, Dictionary and Frozen set



List is a sequence data type in Python. One can store items in the list which are of different types as well.


In day to day applications, development process involves storing sequences of data. List offers such facility.


List can be constructed in several ways:

  • Place all the elements inside square braces separated by commas. Eg: list1=[1,’hello’,8.3].
  • A list can contain another list as a element. Eg: list2=[1,[2,80],[‘D’,3,9.8]]
  • An empty list can be created by simply placing [] without any elements.

We are good to create a list, however simply creating does not help us we need to access the elements of it.  Lists indexing will start from zero. So using index we will call elements of list.

v_list[0] —-> First element. Same indexing will go on.

What will happen when we are looking for an index which does not exist in that list. Obviously interpreter will throw an error : IndexError: list index out of range

And can we ask interpreter to bring an element of index 9.5. Interpreter will inform you that indexes can be only integers but not float type.  list indices must be integers, not float

Well, How to access a list declared inside list with this indexing method. Have a look at below piece of code:

#Declaring list
v_list=[6, ‘hello’, [2,6,’World’]]
#Calling index zero



So by using 2-D indexing we can access list inside list.

We can concatenate strings by accessing them via indexes:

v_list[1]+’World’  —> Output will be Hello World

Till now we are accessing a particular element in the list, however one can access elements in certain range for example elements from 2 to 4 indexes.

Syntax will be : v_list[3:7]

It will start getting elements from 3 till 6, It will not include the value at the second index.

Do try



v_list[:] –> Results in entire list as we did not mention any boundaries.

This is similar to indexing in string data type which we discussed earlier. Recall once that we have used negative indexing in strings, same will be applicable for list as well.  Integer value -1 refers to the last index in a list, the value -2 refers to the second-to-last index in a list, and so on.

v_list[-1] –> Gives last element in the list.

We can change values of elements in list through index.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.