Lists Practice

Lists are like arrays but can store values of different types. A Lists in python don't have fixed size. A List is enclosed in '[' and ']' (square brackets).


In [2]:
myList = [1, 2.0, 'three',4]

You can use print() to print the entire list.


In [3]:
print(myList)


[1, 2.0, 'three', 4]

Or you can use index to print a specific element in the list (remember index always starts from 0)


In [4]:
print(myList[1])


2.0

You can use len() to get the length of a List (number of elements in that list).


In [5]:
len(myList)


Out[5]:
4

Slicing lists using index

You can use ':' to slice a list.

If you want to print a list from a specific element to list use "[x:]". The below example gets all the elements from index 1 to the end of the list.


In [6]:
myList[1:]


Out[6]:
[2.0, 'three', 4]

If you want to get elements from starting to a specific element, use the below format.


In [7]:
myList[:2]


Out[7]:
[1, 2.0]

The above example slices the list from starting up to the 2'nd element (i.e, 0, 1 elements, observe th 2 element is not included).


In [8]:
myList[1:3]


Out[8]:
[2.0, 'three']
You can concatinate lists using '+'.

In [9]:
myList + ["new"]


Out[9]:
[1, 2.0, 'three', 4, 'new']

Here we have not changed the actual list, it is still the same. We've just appended a new item for displaying purpose.

To add an element permanently to the list you can do as following.


In [10]:
myList = myList + ['new']
myList


Out[10]:
[1, 2.0, 'three', 4, 'new']
You can use ' * ' to replicate lists as follows.

In [11]:
myList * 2


Out[11]:
[1, 2.0, 'three', 4, 'new', 1, 2.0, 'three', 4, 'new']

Again the duplication of lists is not permanent, it is just for display purpose.

Basic Lists functions

To add an element to a list (at the end) we can use append().

The syntax is: list.append(item)


In [13]:
lst = [1,2,3,4]
lst


Out[13]:
[1, 2, 3, 4]

In [18]:
lst.append(5)
lst


Out[18]:
[1, 2, 3, 4, 5]

To remove an element from a list we can use pop(). The default working is that it removes the last element in the list. But we can pass the index of element to delete a specific element.


In [19]:
lst


Out[19]:
[1, 2, 3, 4, 5]

In [20]:
lst.pop()


Out[20]:
5

In [21]:
lst


Out[21]:
[1, 2, 3, 4]

In [22]:
lst.pop(1)


Out[22]:
2

In [23]:
lst


Out[23]:
[1, 3, 4]

Python has sort() and reverse() functions which sorts and reverses the list.

Note that these function change the list permanently.


In [25]:
nList = ['s','a','x','b','t']

In [26]:
nList


Out[26]:
['s', 'a', 'x', 'b', 't']

In [27]:
nList.reverse()

In [28]:
nList


Out[28]:
['t', 'b', 'x', 'a', 's']

Notice that the list is reversed permanently.


In [29]:
nList.sort()

In [30]:
nList


Out[30]:
['a', 'b', 's', 't', 'x']

Python sorts the lists in ascending order by default. Works same for number lists also.

If you have a list with different types the sort() will not work and gives you an error.


In [32]:
myList


Out[32]:
[1, 2.0, 'three', 4, 'new']

In [33]:
myList.sort()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-710f4f63be48> in <module>()
----> 1 myList.sort()

TypeError: '<' not supported between instances of 'str' and 'float'

Python has a whole bunch of functions that can work on lists such as clear(), copy(), count(), extend(), index(), insert(), remove()

Nested Lists

Python supports nesting of objects (means object within object). So we can nest a list within a list.


In [34]:
lst1 = [1,2,3]
lst2 = [4,5,6]
lst3 = [7,8,9]

In [36]:
lst1


Out[36]:
[1, 2, 3]

In [37]:
lst2


Out[37]:
[4, 5, 6]

In [38]:
lst3


Out[38]:
[7, 8, 9]

Here we can create a list which contains these three lists. Consider them as multidimendional array.


In [39]:
matrix = [lst1, lst2, lst3]

In [40]:
matrix


Out[40]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

You can access the elements of a nested list using index.


In [41]:
matrix[0]


Out[41]:
[1, 2, 3]

We can use multi level index to access individual elements.


In [42]:
matrix[1][2]


Out[42]:
6