Python List Methods - Comprehensive Guide
Python List Methods
Python List data type provides a set of methods that we can call on the list objects.
In this tutorial, you will learn about all of the Python List methods with description for each of them, and a well detailed example. Also, dedicated tutorials are written for each of the List methods. Link to the respective tutorial is provided at the end of each method.
1. List append() method
Python List append() method appends given object to the end of this list.
In the following program, we take a list of strings in my_list with some initial values. We have to append an object 'mango'
to this list. Call append() method on the list my_list and pass the object 'list'
as argument to the method.
Python Program
my_list = ['apple', 'banana', 'cherry']
my_list.append('mango')
print(my_list)
Output
['apple', 'banana', 'cherry', 'mango']
2. List clear() method
Python List clear() method removes all the items from the list. The method makes the list empty.
In the following program, we take a list my_list which is initialized with [52, 41, 63]
. We have to make this list empty. Call clear() method on the list my_list, and the items in the list shall be cleared (removed).
Python Program
my_list = [52, 41, 63]
my_list.clear()
print(my_list)
Output
[]
3. List copy() method
Python List copy() method creates a shallow copy of the list, and returns the copied list.
In the following program, we take a list my_list with values [22, 44, 66]
. We have to make a copy of this list. Call copy() method on the list my_list, and store the returned value in a variable.
Python Program
my_list = [22, 44, 66]
my_list_copy = my_list.copy()
print(f"Original list : {my_list}")
print(f"Copied list : {my_list_copy}")
Output
Original list : [22, 44, 66]
Copied list : [22, 44, 66]
4. List count() method
Python List count() method counts the number of occurrences of specified value in the list, and returns that count.
In the following program, we take a list my_list with some string values. We have to count the number of occurrences of the value 'apple'
in this list. Call count() method on the list my_list, and pass the value 'apple'
as argument to the method.
Python Program
my_list = ['apple', 'fig', 'apple', 'cherry']
count = my_list.count('apple')
print(f"Count : {count}")
Output
Count : 2
5. List extend() method
Python List extend() method extends the list with items from given iterable.
In the following program, we take a list my_list with some string values. We have to extend this list with items from an another list another_list. Call extend() method on the list my_list, and pass another_list as argument to the method.
Python Program
my_list = ['apple', 'banana', 'cherry']
another_list = ['mango', 'fig']
my_list.extend(another_list)
print(my_list)
Output
['apple', 'banana', 'cherry', 'mango', 'fig']
6. List index() method
Python List index() method is used to find the index (position) of the specified value in the list.
In the following program, we define a list my_list with four string values. We have to find the index of the value 'cherry'
in this list. Call index() method on the list, and pass the value as argument.
Python Program
my_list = ['apple', 'banana', 'cherry', 'fig']
value_index = my_list.index('cherry')
print(f"Index : {value_index}")
Output
Index : 2
7. List insert() method
Python List insert() method inserts given object at specified index in the list.
In the following program, we take a list my_list with some initial values. We have to insert the object 99 at index 3 in this list. Call insert() method on the list my_list, and pass the index and object as arguments to the method.
Python Program
my_list = [11, 22, 33, 44, 55, 66]
my_list.insert(3, 99)
print(my_list)
Output
[11, 22, 33, 99, 44, 55, 66]
8. List pop() method
Python List pop() method removes the object at specified index in the list.
In the following program, we take a list my_list with some initial values. We have to remove the item at index 3 in this list. Call pop() method on the list my_list, and pass the index as argument to the method.
Python Program
my_list = [2, 4, 6, 8, 10, 12]
my_list.pop(3) # index=3
print(my_list)
Output
[2, 4, 6, 10, 12]
9. List remove() method
Python List remove() method removes the first occurrence of specified item from the list.
In the following program, we take a list my_list with some initial values. We have to remove the item with value 8 in this list. Call remove() method on the list my_list, and pass the value 8 as argument to the method.
Python Program
my_list = [2, 4, 6, 8, 10, 12]
my_list.remove(8)
print(my_list)
Output
[2, 4, 6, 10, 12]
10. List reverse() method
Python List reverse() method reverses the order of items in the list in place.
In the following program, we take a list my_list with values [2, 4, 6, 8, 10]
. We have to reverse this list. Call reverse() method on the list my_list.
Python Program
my_list = [2, 4, 6, 8, 10]
my_list.reverse()
print(my_list)
Output
[10, 8, 6, 4, 2]
11. List sort() method
Python List sort() method sorts the items in the list in place in ascending order.
In the following program, we take a list my_list with numeric values. We have to sort this list of numbers in ascending order. Call sort() method on the list my_list, and pass no arguments.
Python Program
my_list = [5, 3, 1, 6, 2, 4]
print(f"Original list : {my_list}")
my_list.sort()
print(f"Sorted list : {my_list}")
Output
Original list : [5, 3, 1, 6, 2, 4]
Sorted list : [1, 2, 3, 4, 5, 6]
Summary
In this Python Lists tutorial, we learned about different methods that we call on list objects.