Python includes many built-in methods for use with list as shown as follows
1. cmp(list1,list2) - It compares the elements of both lists, list1 and list2
2. len(list) - It Returns length of string or set
3. max(list) - It Returns item that has Maximum value in a list
4. min(list) - It Returns item that has Minimum value in a list
5. list(seq) - It converts a tuple into a list
6. list.append(item) - It adds the item to the end of the list
7. list.count(item) - It returns number of times the item occurs in a list
8. list.index(item) - It returns the index number of the item.
9. list.insert(index,item) - It inserts the given item on the the given index number in the list
10 list.reverse() - It reverse the position(index number) of the items in the list
11. list.sort([function]) - It sorts the elements inside the list and uses compare function if provided
12. list.extend(seq) - It adds the elements of the sequence at the end of the list
Lab Assignment:
Write a python script to use frequently used built-in methods(atlest five methods)
a=[40,5,10,20,10]
print(a)
ch='y'
while(ch=='y' or ch=='Y'):
print("1. Min and Max")
print("2. Append Element")
print("3. Insert Element")
print("4. Delete Element")
print("5. Reverse Elements")
print("6. SOrt List")
choice=int(input("Enter Your choice : "))
if(choice==1):
print("Max Element is %d and Min Element is %d" %(max(a),min(a)))
elif(choice==2):
a.append(5)
print(a)
elif choice==3:
a.insert(2,25)
print(a)
elif choice==4:
del a[1]
print(a)
elif choice==5:
a.reverse()
print(a)
elif choice==6:
a.sort()
print(a)
else:
print("Wrong Choice")
ch=input("Want to continue(Y/N) : ")
print("Successful!!")