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)
Or you can use index to print a specific element in the list (remember index always starts from 0)
In [4]:
print(myList[1])
You can use len() to get the length of a List (number of elements in that list).
In [5]:
len(myList)
Out[5]:
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]:
If you want to get elements from starting to a specific element, use the below format.
In [7]:
myList[:2]
Out[7]:
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]:
In [9]:
myList + ["new"]
Out[9]:
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]:
In [11]:
myList * 2
Out[11]:
Again the duplication of lists is not permanent, it is just for display purpose.
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]:
In [18]:
lst.append(5)
lst
Out[18]:
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]:
In [20]:
lst.pop()
Out[20]:
In [21]:
lst
Out[21]:
In [22]:
lst.pop(1)
Out[22]:
In [23]:
lst
Out[23]:
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]:
In [27]:
nList.reverse()
In [28]:
nList
Out[28]:
Notice that the list is reversed permanently.
In [29]:
nList.sort()
In [30]:
nList
Out[30]:
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]:
In [33]:
myList.sort()
Python has a whole bunch of functions that can work on lists such as clear(), copy(), count(), extend(), index(), insert(), remove()
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]:
In [37]:
lst2
Out[37]:
In [38]:
lst3
Out[38]:
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]:
You can access the elements of a nested list using index.
In [41]:
matrix[0]
Out[41]:
We can use multi level index to access individual elements.
In [42]:
matrix[1][2]
Out[42]: