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 9
print("Items in original list : ",len(l))
x = None
y = None
z = -1
print("Reverse of List ",l[x:y:z]) #([starting_point:ending_point:step]) here starting and ending point is none and step is -1
x = -2
y = -9
z = -1
print(l[x:y:z])
#append(item) insert element at the end of the list
l = [ '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)
#extend(sequence)
l = []
l.extend('python')
print(l)
l = ['ghc']
k = [ 'java','c','c++','python']
l.extend(k)
print(l)
#delete item from list
l = [ 5,6,67,5324,324]
print(l)
x = l.pop() #just opposite of append i.e delete the last element in list
print("Item ",x,"is deleted from list")
print(l)
#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)
#remove(item) remove particular item from list
l = [ 'hi','hello','how are you','hi','hi']
print(l)
l.remove('hi')
print(l)
#Common Function on Python List
#sort()-> it will sort list in accending ( list should be homogenous )
#sort(reverse=True)->it will sort in decending order
l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12]
k = l #k uses the same address of l
print(k)
k.sort() #sort the list k
print("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 alphabetically
print(*k)
k.sort(reverse=True)
print(*k)
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 sublist
print(mylist)
#reverse -> Reverse the order of items in the list
l = [ 1,2,3,4,5,6,7]
x = l[::-1] #reverse the list through slicing
print(x)
print(l)
l.reverse()
print(l)
#copy -> Returns a shallow copy of the list
l = [ 1,2,3,4,5]
k = l.copy()
z = l
print(l)
l[2]= 5 #will change only in list l not in their copies
print(k)
print(z)
print(l)
#index -> Returns the index of the first matched item
l = [ 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)
#clear -> Removes all items from the list
mylist = [ 'hello','hi','bye',1,2,3]
print(mylist)
mylist.clear()
print(mylist)
#count -> Returns the count of number of items passed as an argument
l = [ 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)