Python Lists
Python Lists
List is a ordered, indexed and changeable collections of data. List can have heterogenous data type. In python we represent list with square brackets [ ].
Example of list : fruits = ['apple', 'banana', 'mango']
items = ['table', 12, 3.13, 'tree']
Index of list starts from 0. We can check the length if list using len( ) function.
E.g. numbers = [45, 52, 23, 87]
len(numbers) # output : 4
numbers[0] # output : 45
numbers[2] # output : 23
If we want to access the list from last element and do not know the length then we can use negative indexing. -1 index is of last element, -2 index is of second last and so on.
E.g. numbers[-1] #output : 87
numbers[-3] #output : 52
We can add elements to the list using append( ) method. Remember that append will just add the given elements to the list. If you give the another list to the first list's append( ) function then it will consider that second list as a single element. append( ) takes only one arguement.
E.g. list1 = [1, 2, 3]
list1.append(4) # output : list1 = [1, 2, 3, 4]
list1.append([10, 20, 30]) #output : list1 = [1, 2, 3, 4, [10, 20, 30]]
So if you want to add multiple elements to the list then use extend( ) function.
E.g. list1.extend([5, 6, 7, 8]) # output : lis1 = [1, 2, 3, 4, [10, 20, 30], 5, 6, 7, 8]
What if we want to add element at specified position ?
E.g. list1 = [10, 20, 30, 40]
list1.insert(2, 100) #output : list1 = [10, 20, 100, 30, 40]
First arguement in insert( ) function is the index position and second arguement is the item value.
To remove the particular item from the list use remove( ) method. In remove( ) method we give the name of item which we want to remove.
E.g. list2 = [10, 20 , 30]
list2.remove(20) # output : list2 = [10 , 30]
If we want to remove the elements using index of items we can use pop( ) method. In pop( ) method we give the index of item. If we do not give any arguement then it will remove the last item from the list.
E.g. list3 = [11, 12, 13, 14]
list3.pop( ) #output : list3 = [11, 12, 13]
list3.pop(0) #output : list3 = [12, 13]
clear( ) method will remove the all items from the list. Remember that this function will not delete the list, it will make the list empty.
We can join two or more list using + operator.
E.g. list1 = [1, 2, 3]
list2 = [5, 6, 7]
list3 = list1 + list2 #output : list3 = [1, 2, 3, 5, 6, 7]
There is inbuilt function in python named list( ). This is the contructor which is used to create the list.
E.g. my_list = list((1, 2, 3, 4))
What if we multiply the whole list with an integer?
E.g. list4 = [10, 20, 30]
list4*3 #output : list4 = [10, 20, 30, 10, 20, 30, 10, 20, 30]
Slicing in List :
We can slice the list (take out a part from list) using the index of items. We use colon : to slice the list.
E.g. list5 = [1, 2, 3, 4, 5, 6]
list5[2:4] # output : [3, 4]
First number before colon is the starting index which is inclusive, second number which after colon is the stopping index which is exclusive means we can slice only before that element as you can see in above example.
What if we do not give any index before or after the colon?
If we do not give any element before colon then it will include the all elements from starting to the stopping point. If we do not give the stopping index then it will include all elements till the last if list.
E.g. list6 = [10, 20, 30, 40, 50]
list6[:3] #output : [10, 20, 30]
list6[2:] #output : [30, 40, 50]
We can use max( ) function to get the maximum from the list and min( ) function to get the minimum from the list.
E.g. max(list6) #output : 50
min(list6) #output : 10
To sort the list use inbuilt sort( ) function.
E.g. list7 = [34, 12, 78, 43]
list7.sort( ) #output : list7 = [12, 34, 43, 78]
To reverse the list use inbuilt reverse( ) function.
E.g. list8 = [21, 31, 41, 51]
list8.reverse( ) # output : list8 = [51, 41, 31, 21]
If we assign the one list to another variable the both list will point to the same memory location. Now the problem is that if you make any changes to the one list then it will also reflect to another list.
E.g. list9 = [1, 2, 3, 4, 5]
list10 = list9
list10.append(50)
print(list9) # output : list9 = [1, 2, 3, 4, 5, 50]
print(list10) # output: list10 = [1, 2, 3, 4, 5, 50]
The solution is that we can use inbuilt copy( ) function for this purpose.If we use copy( ) function then the changes made in one list will not reflect to the another because they both will point to different memory locations and have their own list , no sharing.
E.g. list11 = [1, 2, 3, 4, 5]
list12 = list11.copy( )
list12.append(100)
print(list12) # output : list2 = [1, 2, 3, 4, 5, 100]
print(list11) # output : list1 = [1, 2, 3, 4, 5]
Comments
Post a Comment