10 most useful list methods in Python (20/100 Days of Python)

Martin Mirakyan
3 min readJan 21, 2023

--

Day 20 of the “100 Days of Python” blog post series covering the most useful list methods

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.

  1. 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].
  2. 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].
  3. 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].
  4. 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].
  5. 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].
  6. 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.
  7. 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.
  8. 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].
  9. 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].
  10. 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) # []

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet