Python list methods are built-in functions that can be used to manipulate and perform operations on lists. These methods allow you to add, remove, or modify elements in a list, as well as perform other operations such as sorting, reversing, and copying. Some of the most commonly used Python list methods include append()
, extend()
, insert()
, remove()
, pop()
, sort()
, reverse()
, and index()
. Details about creating and accessing list can be read from this article. In this article, we will be presenting a detailed overview of the methods available for Python lists, along with explanations and examples:
Adding Elements
There are several methods you can use to add elements to a list in Python:
Append()
The append()
method adds an element to the end of a list:
my_list = [1, 2, 3] my_list.append(4) print(my_list) # Output: [1, 2, 3, 4]
Extend()
The extend()
method appends the elements of an iterable object (e.g. a list, tuple, or string) to the end of a list:

my_list = [1, 2, 3] my_list.extend([4, 5, 6]) print(my_list) # Output: [1, 2, 3, 4, 5, 6]
Insert()
The insert()
method inserts an item at a defined index.
my_list = [1, 2, 3] my_list.insert(0, 5) print(my_list) # Output: [5, 1, 2, 3]
Removing Elements
There are several methods you can use to remove elements from a list in Python:
Pop()
The pop()
method removes and returns the element at a given index. If no index is provided, it removes and returns the last element:
my_list = [1, 2, 3, 4, 5] x = my_list.pop(2) print(my_list) # Output: [1, 2, 4, 5] print(x) # Output: 3
Remove()
The remove()
method removes the first occurrence of a given element from a list:
my_list = [1, 2, 3, 4, 5] my_list.remove(3) print(my_list) # Output: [1, 2, 4, 5]
Clear()
The clear()
method removes all elements from a list:
my_list = [1, 2, 3, 4, 5] my_list.clear() print(my_list) # Output: []
Del
The del
statement can be used to remove elements from a list by index or slice:
my_list = [1, 2, 3, 4, 5] del my_list[2] print(my_list) # Output: [1, 2, 4, 5] del my_list[1:3] print(my_list) # Output: [1, 5]
Modifying Elements
You can modify individual elements of a list using indexing:
my_list = [1, 2, 3, 4, 5] my_list[2] = 6 print(my_list) # Output: [1, 2, 6, 4, 5]
You can also modify elements using slicing:
my_list = [1, 2, 3, 4, 5] my_list[1:3] = [6, 7, 8] print(my_list) # Output: [1, 6, 7, 8, 4, 5]
Other List Methods
Index()
The index()
method returns the index of the first occurrence of a given element in a list:
my_list = [1, 2, 3, 4, 5] x = my_list.index(3) print(x) # Output: 2
Count()
The count()
method returns the number of times a given element appears in a list:
my_list = [1, 2, 2, 3, 3, 3, 4, 5] x = my_list.count(3) print(x) # Output: 3
Sort()
The sort()
method sorts the elements of a list in ascending order:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5] my_list.sort() print(my_list) # Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
Reverse()
The reverse()
method reverses the order of the elements in a list:
my_list = [1, 2, 3, 4, 5] my_list.reverse() print(my_list) # Output: [5, 4, 3, 2, 1]
Copy()
The copy()
method returns a shallow copy of a list:
my_list = [1, 2, 3, 4, 5] new_list = my_list.copy() print(new_list) # Output: [1, 2, 3, 4, 5]
Extend()
The extend()
method appends the elements of an iterable to the end of a list:
my_list = [1, 2, 3] my_list.extend([4, 5, 6]) print(my_list) # Output: [1, 2, 3, 4, 5, 6]
insert()
The insert()
method inserts an element at a given index:
my_list = [1, 2, 3] my_list.insert(0, 0) print(my_list) # Output: [0, 1, 2, 3]
Sort()
The sort()
method can also sort a list in descending order by passing the reverse=True
parameter:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5] my_list.sort(reverse=True) print(my_list) # Output: [9, 6, 5, 5, 4, 3, 2, 1, 1]
Join()
The join()
method can be used to concatenate the elements of a list into a string:
my_list = ['a', 'b', 'c'] result = ''.join(my_list) print(result) # Output: 'abc'
Split()
The split()
method can be used to split a string into a list of substrings:
my_string = 'a,b,c' my_list = my_string.split(',') print(my_list) # Output: ['a', 'b', 'c']
Map()
The map()
function can be used to apply a function to each element of a list:
my_list = [1, 2, 3, 4, 5] result = map(lambda x: x * 2, my_list) print(list(result)) # Output: [2, 4, 6, 8, 10]
If you practice using all of the Python list methods covered in this guide, you will have a strong foundation in list manipulation and be able to perform a wide variety of operations on lists. Whether you are working on a simple script or a large-scale application, understanding and utilizing these methods will help you write more efficient and effective code. With consistent practice and application, you can become proficient in working with Python lists and leverage their power to build robust and dynamic programs. Also, practise about python list functions from this link as your next step.
The codes mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information.