Python contains the following list methods.
used to add the new element at the end of the list.
fruit_list=["Apple","Orange","Mango"] print("Fruits list :",fruit_list) #this line will add Cherry at the end of list fruit_list.append("Cherry") print("New Fruits list :",fruit_list) """ ***Output*** Fruits list : ['Apple', 'Orange', 'Mango'] New Fruits list : ['Apple', 'Orange', 'Mango', 'Cherry'] """
used to empty the list.
fruit_list=["Apple","Orange","Mango"] print("Fruits list :",fruit_list) #this line will empty the list fruit_list.clear() print("New Fruits list :",fruit_list) """ ***Output*** Fruits list : ['Apple', 'Orange', 'Mango'] New Fruits list : [] """
counts the number of occurrence of particular item in a list.
number_list=[10,16,40,50,60,40,16,30,16,10,50] print("Numbers list :",number_list) print("Total Count of 16 :",number_list.count(16)) """ ***Output*** Numbers list : [10, 16, 40, 50, 60, 40, 16, 30, 16, 10, 50] Total Count of 16 : 3 """
copies the elements of one list into another.
list1=["Apple","Orange","Mango"] print("List1 items:",list1) #this line will copy list1 items into list2 list2=list1.copy() print("List2 items:",list2) """ ***Output*** List1 items: ['Apple', 'Orange', 'Mango'] List2 items: ['Apple', 'Orange', 'Mango'] """
used to join two list.
list1=["Apple","Orange","Mango"] list2=["Cherry","Grapes","Melon"] #this line will join list1 and list2 list1.extend(list2) print("list1 items") print(list1) """ ***Output*** list1 items ['Apple', 'Orange', 'Mango', 'Cherry', 'Grapes', 'Melon'] """
returns the lowest index of given element.
number_list=[10,30,16,50,60,40,16,30,16,10,50] print("Index of 16 :",number_list.index(16)) """ ***Output*** Index of 16 : 2 Note:16 is present at index 2 , 6 and 8 """
used to add new items into list at particular index.
fruit_list=["Apple","Orange","Mango"] print("Before insertion") print(fruit_list) #this line will add Banana at index 1 fruit_list.insert(1,"Banana") print("After insertion") print(fruit_list) """ ***Output*** Before insertion ['Apple', 'Orange', 'Mango'] After insertion ['Apple', 'Banana', 'Orange', 'Mango'] """
fruit_list=["Apple","Orange","Mango","Cherry"] print("Fruits list :",fruit_list) #this line will delete last element Cherry fruit_list.pop() print("Fruits list :",fruit_list) #this line will delete element at index 1(Orange) fruit_list.pop(1) print("Fruits list :",fruit_list) """ ***Output*** Fruits list : ['Apple', 'Orange', 'Mango', 'Cherry'] Fruits list : ['Apple', 'Orange', 'Mango'] Fruits list : ['Apple', 'Mango'] """
used to delete or remove item from list.
fruit_list=["Apple","Orange","Mango"] print("Before deletion") print(fruit_list) #this line will delete Orange from list fruit_list.remove("Orange") print("After deletion") print(fruit_list) """ ***Output*** Before deletion ['Apple', 'Orange', 'Mango'] After deletion ['Apple', 'Mango'] """
reverses elements of the list.
fruit_list=["Apple","Orange","Mango","Cherry"] print("Fruits list :",fruit_list) #this line will reverse the list items fruit_list.reverse() print("Reverse Fruits list :",fruit_list) """ ***Output*** Fruits list : ['Apple', 'Orange', 'Mango', 'Cherry'] Reverse Fruits list : ['Cherry', 'Mango', 'Orange', 'Apple'] """
Sorts the list.
#Example 1:Ascending order str_list=["Apple","Orange","Mango","Cherry"] int_list=[58,20,46,36] char_list=['E','A','S','Y'] print("List item before sorting") print(str_list) print(int_list) print(char_list) #Sort the lists in ascending order str_list.sort() int_list.sort() char_list.sort() print("List item after sorting") print(str_list) print(int_list) print(char_list) """ ***Output*** List item before sorting ['Apple', 'Orange', 'Mango', 'Cherry'] [58, 20, 46, 36] ['E', 'A', 'S', 'Y'] List item after sorting ['Apple', 'Cherry', 'Mango', 'Orange'] [20, 36, 46, 58] ['A', 'E', 'S', 'Y'] """ #Example 2:Descending order str_list=["Apple","Orange","Mango","Cherry"] int_list=[58,20,46,36] char_list=['E','A','S','Y'] print("List item before sorting") print(str_list) print(int_list) print(char_list) #Sort the lists in descending order str_list.sort(reverse=True) int_list.sort(reverse=True) char_list.sort(reverse=True) print("List item after sorting descending order") print(str_list) print(int_list) print(char_list) """ ***Output*** List item before sorting ['Apple', 'Orange', 'Mango', 'Cherry'] [58, 20, 46, 36] ['E', 'A', 'S', 'Y'] List item after sorting descending order ['Orange', 'Mango', 'Cherry', 'Apple'] [58, 46, 36, 20] ['Y', 'S', 'E', 'A'] """