10 most useful list methods in Python (20/100 Days of Python)
3 min readJan 21, 2023
Python provides a wide range of built-in methods for working with lists, making it easy to manipulate and transform data. In this tutorial, we will take a look at some of the most useful list methods in Python.
list.append(item)
: This method is used to add an item to the end of a list. For example,numbers = [1, 2, 3]; numbers.append(4)
will add the number 4 to the end of the list. So, numbers will be[1, 2, 3, 4]
.list.extend(iterable)
: This method is used to add multiple items to the end of a list. The argument passed to the method should be an iterable (e.g. a list or a tuple). For example,numbers = [1, 2, 3]; numbers.extend([4, 5, 6])
will add the numbers 4, 5, and 6 to the end of the list. This is also possible to do withnumbers += [4, 5, 6]
. In both cases, the list numbers will have[1, 2, 3, 4, 5, 6]
.list.insert(index, item)
: This method is used to insert an item at a specific index in a list. For example,numbers = [1, 2, 3]; numbers.insert(1, 4)
will insert the number 4 at index 1 in the list. So, the list will have[1, 4, 2, 3]
.list.remove(item)
: This method is used to remove the first occurrence of a specific item from a list. For example,numbers = [1, 2, 3, 4, 4]; numbers.remove(4)
will remove the first occurrence of the number 4 from the list. In this case, the listnumbers
will become[1, 2, 3, 4]
.list.pop(index)
: This method is used to remove an item at a specific index in a list and return the removed item. For example,numbers = [1, 2, 3]; numbers.pop(1)
will remove the item at index 1 (2) and return it. In case no index is specified, thepop
function removes the last element from the list. In our example, the list numbers after performingpop(1)
will become[1, 3]
.list.index(item)
: This method is used to find the index of the first occurrence of a specific item in a list. In case ofnumbers = [1, 2, 3, 4, 4]; numbers.index(4)
will return the index of the first occurrence of the number 4 in the list. In this case the returned index will be3
.list.count(item)
: This method is used to count the number of occurrences of a specific item in a list. For example,numbers = [1, 2, 3, 4, 4]; numbers.count(4)
will return the number of occurrences of the number 4 in the list — which is 2 in this example.list.sort()
: This method is used to sort the items in a list in ascending order. For example,numbers = [3, 1, 4, 2]; numbers.sort()
will sort the list in ascending order and the list will become[1, 2, 3, 4]
.list.reverse()
: This method is used to reverse the order of the items in a list. For example,numbers = [1, 2, 3]; numbers.reverse()
will reverse the order of the items in the list. So, thenumbers
list will turn into[3, 2, 1]
.list.clear()
: This method is used to remove all items from a list, effectively emptying it. For example,numbers = [1, 2, 3]; numbers.clear()
will remove all items from the list and make it an empty list.
You can also use del keyword to remove all items in a list but the difference is thatdel
keyword is used to delete the list variable entirely from the memory whilelist.clear()
only empties the list.
numbers = [1, 2, 3]
del numbers
# numbers variable doesn't exist anymore
numbers = [1, 2, 3]
numbers.clear()
print(numbers) # []
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course — apply some of the list methods on different problems
- Full series: 100 Days of Python
- Previous topic: Splitting and Joining strings
- Next topic: Nested loops in Python