append()
This method is used to append an element to the list.
Example:
num_list=[1,2,3,4,5] num_list.append(6) print(num_list)
Output:
[1, 2, 3, 4, 5, 6]
count()
This method is used to count the number of times an element appears in the list.
Example:
list1=["apple","apple","mango","orange","litchi"] count1=list1.count("apple") print(count1)
Output:
2
pop()
This method is used to remove the element at the specified index from the list. Index is an optional parameter. If no index is specified, then remove the last object (or element) from the list.
Example:
list1=[2,4,5,6,7] list1.pop() print(list1)
Output:
[2, 4, 5, 6]
remove()
It is used to remove or delete obj or element from the list. ValueError is generated if obj is not present in the list. If multiple copies of obj exists in the list, then the first value is deleted.
Example:
list1=["red","green","blue","yellow"] list1.remove("red") print(list1)
Output:
['green', 'blue', 'yellow']
reverse()
This method is used to reverse the elements in the list.
Example:
list1=["red","green","blue","yellow"] list1.reverse() print(list1)
Output:
['yellow', 'blue', 'green', 'red']
sort()
This method is used to sort the elements in the list.
Example:
list2=[90,10,60,40,70,25] list2.sort() print(list2)
Output:
[10, 25, 40, 60, 70, 90]
sum()
This method is used to add the values in the list that has numbers.
Example:
list2=[90,10,60,40,70,25] sum1=sum(list2) print("Total=",sum1)
Output:
Total= 295
max()
This method is used to return the maximum value in the list.
Example:
list2=[90,10,60,40,70,25] maximum=max(list2) print("Maximum=",maximum)
Output:
Maximum= 90
min()
min() method is used to return the minimum value in the list.
Example:
list2=[90,10,60,40,70,25] minimum=min(list2) print("Minimum=",minimum)
Output:
Minimum= 10
len()
This method is used to return the length of the list.
Example:
list1=["a","b","c","d","e","f","g"] list_length=len(list1) print("List length=",list_length)
Output:
List length= 7
Leave a comment