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 withÂnumbers += [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 listÂnumbers
 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, theÂpop
 function removes the last element from the list. In our example, the list numbers after performingÂpop(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 ofÂnumbers = [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 beÂ3
.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.
Get Martin Mirakyan’s stories in your inbox
Join Medium for free to get updates from this writer.
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, theÂnumbers
 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 thatÂdel
 keyword is used to delete the list variable entirely from the memory whileÂlist.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) # []