Python list : tips and tricks
The Python list is another built-in data type of Python.It is similar to tuple in a way that it can store many values in a sequence.Unlike tuple list are mutable sequence.
LinK :Python mutable and immutable
The values of list is written inside the subscript [](the square bracket) and they are separated with comas.How to create list object is shown below.
Link :Python tuple
>>> [1,2,3] #a list >>> list1=[13 ,4 ,56] #another list >>> list1 [13 ,4 ,56] >>> [ ] #an empty list >>> list('Text') #list created from string ['T', 'e', 'x', 't'] >>> list2=list((23 , 5 , 3.14 , "text")) #list created from tuple >>> list2 [23, 5, 3.14, 'text']
Accessing list values
To access the list object values we will use an index value which is written inside the subscript([]).The index value count begins from 0 if we start counting from the left side but if we start counting the index from the right side than it begins from -1.Consider the code below.
>>> list1=[13 , 4 , 56 , 890] >>> list1[0] 13 >>> list1[2] 56 >>> list1[-1] 890 >>> list1[-3] 4
Slicing list
Like any other built-in type sequence list can be sliced from any position.The formula to slice list is same as that of the string.Certain things to note for slicing list:
We will use some index values to slice the list and it follows the format.
string_object_name[start:stop:step]
The ‘start‘ index value signify the position from where the slicing will begin.
The ‘stop‘ index value signify the position where the slicing will end.
The ‘step‘ index value signify the number of values which should be jump between the ‘start‘ and ‘stop‘ index value.An example will clarify this.
The colon ‘:‘ after each index value is important!
You can mention only the ‘start’ index or ‘stop’ index or both or all the three and depending on that the string will be sliced accordingly.
>>> lists=[12, 5 , 00 , 'Happy' , 34 , 67.89 , 102 ] >>> lists[3] #access the 4th values 'Happy' >>> lists[3:] #slice the list starting from 4th value ['Happy', 34, 67.89, 102] >>>
If the colon (:) is placed before the index value,the index value represent the position where the slicing will end.
>>> lists=[12, 5 , 00 , 'Happy' , 34 , 67.89 , 102 ] >>> lists[:5] #slicing begins from 0th and ends at 5th value [12, 5, 0, 'Happy', 34] >>> lists[:] #all the values are copied [12, 5, 0, 'Happy', 34, 67.89, 102]
The next example uses both ‘start’ and ‘stop’ index.
>>> lists=[12, 5 , 00 , 'Happy' , 34 , 67.89 , 102 ] >>> lists[3:6] #slicing begin at 3rd index and end at 2 index before 6th index ['Happy', 34, 67.89] >>> lists[-5:-2] #slicing begin at -5th and ends at value before -2th index [0, 'Happy', 34]
Using the three index ‘start’,’stop’ and ‘step’.
>>> lists[0:6:2] [12, 0, 34]
In using the three indexes slicing begin from 0th position and ends at 6th position but only the values lying at 0 , (0+2) , (0+2+2) are taken and (0+2+2+2=6) is not considered because slicing ends just before the ‘stop'(6th) index .
List concatenating
We can concatenate two lists using the ‘+'(plus) sign.
>>> new_list=lists+[23 , 'New' , 00] >>> new_list [12, 5, 0, 'Happy', 34, 67.89, 102, 23, 'New', 0]
What if we use ‘*’ sign,in this case the list is concatenated (not multiplied) with its own values the number of times represented by the number following the ‘*’ sign.
>>> ls=lists * 2 >>> ls [12, 5, 0, 'Happy', 34, 67.89, 102, 12, 5, 0, 'Happy', 34, 67.89, 102]
Nesting lists
Nesting list is also possible.
>>> lists=[12, 5 , 00 , 'Happy' , 34 , 67.89 , 102 ] >>> ls=['D' , 'New' , 90] >>> Nes=[ ls , 'Text' , lists] >>> Nes [['D', 'New', 90], 'Text', [12, 5, 0, 'Happy', 34, 67.89, 102]]
Note the values of ‘ls’ and ‘lists’ are written inside ‘[]’ this shows that the lists are nested.
To know more about operations that can be performed with list object visit the link Python list object methods.