Professional Documents
Culture Documents
LIST METHODS
i. list():
list=list(range(0,10,2))
print(list)
ii) len():
n=[10,20,30,40]
print(len(n))
iii) count():
n=[1,2,2,2,2,3,3]
print(n.count(1))
print(n.count(2))
iv) index():
n=[1,2,2,2,2,3,3]
print(n.index(1))
v) append():
*We can use append() function to add item at the end of the list.
*By using this append function, we always add an element at last position.
list=[]
list.append("A")
list.append("B")
print(list)
vi) insert():
*It is used to insert item at specified index position.
n=[1,2,3,4,5]
n.insert(1,888)
print(n)
vii) extend():
*If we waant to add all items of one list to another list,we use extend() method.
order1=["Chicken","Mutton","Fish"]
order2=["RC","KF","FO"]
order1.extend(order2)
print(order1)
viii) remove():
* We can use this function to remove specified item from the list.
*If the item present multiple times then only first occurrence will be removed.
n=[10,20,40,30]
n.remove(10)
print(n)
ix) pop():
*This is only function which manipulates list and returns some element
n=[10,20,30,40]
print(n.pop(0))
print(n.pop(1))
print(n)
x) reverse():
n.reverse()
print(n)
xi) sort():
*If you want to sort the elements of list according to default natural sorting order then we
should go for sort() method.
n=[20,5,15,10,0]
n.sort()
print(n)
xii) copy():
x=[10,20,30,40]
y=x.copy()
y[1]=777
print(x)
print(y)
xiii) clear():
n=[10,20,30,40]
print(n)
n.clear()
print(n)