List in Python
List in Python is a very powerful data structure which can store any type of data which is supported by Python.
It is created using square bracket for example
l=[2,4,5.2,’post’,’network’,10]
this statement creates a list object of class list, and indexes objects as l[0], l[1], l[2], l[3], l[4] and l[5].
If you check type of l i.e >>> type(l)
it will show the the type class ‘list’ i.e. l is object of class list.
However, if you check type of l[0] i.e.
>>>type(l[0])
then it will show class ‘int’.
If you check type of l[2] i.e.
>>>type(l[2])
then it will show class ‘float’.
If you check type of l[3] i.e.
>>>type(l[3])
then it will show class ‘str’ i.e string class.
This means that Python is aware about what types are data are stored in the list l.
Unlike, an array of C\C++ and Java, list of Python can store different type of data i.e integer, float and string.
Slicing of a List in Python
Accessing portion of a list is called slicing of a list. : symbol is used to slice a list in which at the left side is start index and right side is end index i.e start:end-1(in end index 1 will be subtracted).
For example if you want to access element from index 2 to 4 from the list l=[2,4,5.2,’post’,’network’,10].
Then you will use variable l[2:5] and the output would be.
[5.2, ‘post’]
List is Mutable in Python
Mutability refers to change value of an element from a data structure. See the example
You can change value of l[0] which is now 2 using an assignment statement.
That is l[0]= 8
This is no allowed in tuples in python i.e tuple in Python is immutable.
Accessing an Element from a List
Indexing of a list is started from 0.
That is if you want to access the first item of the list l this the will be l[0].
If you use print(l[0]) then the output would be 2.
If you want to access the third item of the list l this the will be l[2].
If you use print(l[2]) then the output would be 5.2.
If you want to access the fourth item of the list l this the will be l[3].
If you use print(l[3]) then the output would be ‘post’.
Nested List
In Python, list can be nested at any level.
Suppose you create two lists within a list nl i.e. nl=[[1,2,3],[4,5,6]].
Then 1 can be accessed using indexed variable nl[0][0].
2 can be accessed using indexed variable nl[0][1].
3 can be accessed using indexed variable nl[0][2].
Then 4 can be accessed using the indexed variable nl[1][0].
5 can be accessed using indexed variable nl[1][1].
6 can be accessed using indexed variable nl[1][2].
See more complex list nnl=[[1,2,3],[4,[4.1,4.2],6],[7,8,9]].
Then 1 can be accessed using indexed variable nnl[0][0]
Then 2 can be accessed using indexed variable nnl[0][1]
Then 3 can be accessed using indexed variable nnl[0][2]
Then 4 can be used using indexed variable nnl[1][0]
You will be interested in accessing 4.1 and 4.2.
Then 4.1 can be accessed using indexed variable nnl[1][1][0].
Then 4.1 can be accessed using indexed variable nnl[1][1][1].
Then 1 can be accessed using indexed variable nnl[2][0].
Then 2 can be accessed using indexed variable nnl[2][1].
Then 3 can be accessed using indexed variable nnl[2][2].