Lists are one of the most powerful tools in Python.
They are just like the arrays declared in other languages.
But the most powerful thing is that list need not be always homogeneous.
A single list can contain strings, integers, as well as objects.
Lists can also be used for implementing stacks and queues.
Lists are mutable, i.e., they can be altered once declared.
Example - 1:
# Declaring and printing list L = [1,"a","string",1+2] print(L)
[1, 'a', 'string', 3]
Example - 2:
l = [ 'hello','hi','how are you' ] # Homogenous List(contains element of same type)# 0 1 2 (indexing)print(l)print(l[2])#print element at index 2l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12] #Homogenous listprint("Homogeneous List :",l)print(l[5])l = [ 'hello','hi',3,4.5 ] #Non-homogeneous List (contains element of differnt type)# -4 -3 -2 -1print("Non-Homogeneous List : ",l)print(l[-3])
l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12]print(l[2:10])#SLICING (print element from index 0 to n-1) here it prints element from index 2 to 9print("Items in original list : ",len(l))x =Noney =Nonez =-1print("Reverse of List ",l[x:y:z]) #([starting_point:ending_point:step]) here starting and ending point is none and step is -1
x =-2y =-9z =-1print(l[x:y:z])
[45, 12, 67, 12, 43, 1, 6, 8]
Items in original list : 13
Reverse of List [12, 33, 6, 8, 6, 1, 43, 12, 67, 12, 45, 23, 56]
[33, 6, 8, 6, 1, 43, 12]
Operation on a list to Add items
append
insert
extend
To Delete items
pop
remove
To Common Operation
sort
copy
reverse
count
index
clear
Build_in Functions to Add Items
Example - 5:
#append(item) insert element at the end of the listl = [ 'hi',3.14,'how']l.append('item')print(l)l.append(122335)print(l)k = [ 9,8,7,6,5,4,3,]l.append(k)print(l)
#insert(index,item) insert item at given index l = [ 'hello',3.14,1452,432423,434,234]l.insert(2,'hi')print(l)
['hello', 3.14, 'hi', 1452, 432423, 434, 234]
Example - 7:
#extend(sequence) l = []l.extend('python')print(l)
['p', 'y', 't', 'h', 'o', 'n']
Example - 8:
l = ['ghc']k = [ 'java','c','c++','python'] l.extend(k)print(l)
['java', 'c', 'c++', 'python']
Build_in Functions to Remove Items
Example - 9:
#delete item from listl = [ 5,6,67,5324,324]print(l)x = l.pop()#just opposite of append i.e delete the last element in listprint("Item ",x,"is deleted from list")print(l)
[5, 6, 67, 5324, 324]
Item 324 is deleted from list
[5, 6, 67, 5324]
Example - 10:
#pop(index)l = [10,9,9,34,2,43213,21]print(l)k = l.pop(5)print("item at index 5 is {} is deleted".format(k))print(l)
[10, 9, 9, 34, 2, 43213, 21]
item at index 5 is 43213 is deleted
[10, 9, 9, 34, 2, 21]
Example - 11:
#remove(item) remove particular item from listl = [ 'hi','hello','how are you','hi','hi']print(l)l.remove('hi')print(l)
['hi', 'hello', 'how are you', 'hi', 'hi']
['hello', 'how are you', 'hi', 'hi']
Practice Problems:
Example - 12:
#Common Function on Python List#sort()-> it will sort list in accending ( list should be homogenous )#sort(reverse=True)->it will sort in decending orderl = [ 56,23,45,12,67,12,43,1,6,8,6,33,12]k = l #k uses the same address of lprint(k)k.sort()#sort the list kprint("Sorted k : ",k)print(l)l.sort(reverse=True)print("Sorted l : ",l)
l = [ 'zebra','apple','something','eye','flag','computer','python']k = [ 'zebra','apple','something','eye','flag','computer','python']print(*l)#prints only elements in list l l.sort()print(*l)#print after sorting alphabeticallyprint(*k)k.sort(reverse=True)print(*k)
zebra apple something eye flag computer python
apple computer eye flag python something zebra
zebra apple something eye flag computer python
zebra something python flag eye computer apple
Example - 14:
mylist = [ ['hi',10,32], ['abc',100,2,3,], ['something',5,6,7,8], ['popcorn',9,10,11,12]]print(mylist)mylist.sort()#sort according to the element at index 0 of every sublistprint(mylist)
#reverse -> Reverse the order of items in the listl = [ 1,2,3,4,5,6,7]x = l[::-1]#reverse the list through slicingprint(x)print(l)l.reverse()print(l)
[7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
Example - 16:
#copy -> Returns a shallow copy of the listl = [ 1,2,3,4,5]k = l.copy()z = lprint(l)l[2]=5#will change only in list l not in their copiesprint(k)print(z)print(l)
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 5, 4, 5]
[1, 2, 5, 4, 5]
Example - 17:
#index -> Returns the index of the first matched iteml = [ 12,3,4532,3213,45,423,453241,53,42,4542,4,63243,4542,5,34,3445,3,34,34,234,324]k = l.index(4542)print(k)print(l[k+2])l = [ 'hello','hi',12,23,23,[1,23],'hi']k = l.index('hi')j = l.index([1,23])print(k)print(j)
9
63243
1
5
Example - 18:
#clear -> Removes all items from the listmylist = [ 'hello','hi','bye',1,2,3]print(mylist)mylist.clear()print(mylist)
['hello', 'hi', 'bye', 1, 2, 3]
[]
Example - 19:
#count -> Returns the count of number of items passed as an argumentl = [ 1,6,3,2,1,34,64,1,3,5654,1,344,62,1,346,3,1,35464,1,35235,1,4352,3]print(l)x = l.count(3)y = l.count(1)print(x)print(y)