Hello everyone
In this tutorial, we will learn about methods that are used on lists in Python by using some simple examples which helps you to understand easily.
Python List Methods are the built-in methods in lists that are used to perform operations on Python lists.
Below, we’ve explained all the Python list methods for example, append(), copy(), insert(), and many more.
Python List methods
Python has a set of built-in methods that you can use on lists. Let’s look at some different list methods in Python for Python lists:
- append() : Used for adding elements at the end of the list.
Example
Add an element to the vegetables list:
vegetables = ['potato', 'tomato', 'carrot'] vegetables.append("onion") print(vegetables)
Output :
['potato', 'tomato', 'carrot', 'onion']
- clear() : Used for removing all items from the list.
Example
Remove all elements from the vegetables list:
vegetables = ['patato', 'tomato', 'carrot', 'onion'] vegetables.clear() print(vegetables)
Output :
[]
- copy() : It returns a copy of a list.
Example
copy the vegetables list:
vegetables = ['potato', 'tomato', 'carrot', 'onion'] x = vegetables.copy() print(x)
Output :
['potato', 'tomato', 'carrot', 'onion']
- count() : Returns the number of elements with a specified value.
Example
Return the number of times the value “carrot” appears in the vegetables list:
vegetables = ['potato', 'tomato', 'carrot', 'onion'] x = vegetables.count("carrot") print(x)
Output :
1
- extend() : Adds each elements of a list ( or any iterable), to the end of the current list.
Example
Add elements of fruits to the vegetables list:
vegetables = ['potato', 'tomato', 'carrot'] fruits = ['apple', 'banana', 'mango'] vegetables.extend(fruits) print(vegetables)
Output :
['potato', 'tomato', 'carrot', 'apple', 'banana', 'mango']
- index() : Returns the index of the first element with the specified value.
Example
what is the index value or position of tomato:
vegetables = ['potato', 'tomato', 'carrot'] x = vegetables.index("tomato") print(x)
Output:
1
- insert() : Inserts a given element at a given index in a list.
Example
Insert the value onion as the third element of the vegetables list:
vegetables = ['potato', 'tomato', 'carrot'] vegetables.insert(2, "onion") print(vegetables)
Output :
['potato', 'tomato', 'onion', 'carrot']
- pop() : Removes and returns the last value from the list or the given index value.
Example
remove the second element of the vegetables list:
vegetables = ['patato', 'tomato', 'carrot'] vegetables.pop(1) print(vegetables)
Output:
['potato', 'carrot']
- remove() : Removes the first item with the specified value.
Example
Remove “potato” element from the vegetables list:
vegetables = ['potato', 'tomato', 'carrot'] vegetables.remove("potato") print(vegetables)
Output :
['tomato', 'carrot']
- reverse() : Reverses the order of the list.
Example
Reverse the order of vegetables list:
vegetables = ['potato', 'tomato', 'carrot'] vegetables.reverse() print(vegetables)
Output :
['carrot', 'tomato', 'potato']
- sort() : sort a list in ascending, descending, or user-defined order. In other words it is used to sort the list.
Example
Sort the vegetables list alphabetically:
vegetables = ['potato', 'tomato', 'carrort'] vegetables.sort()
Output :
['carrot', 'potato', 'tomato']