In this tutorial, you’ll learn how to use Python list methods to add elements to, modify, and remove elements from Python lists.

When you’re starting out with programming in Python, lists are one of the first built-in data structures that you’ll learn about. Over the next few minutes, we’ll review the basics of Python lists and then go over several useful list methods that you can use when working with lists.

 Let’s begin!

An Overview of Python Lists

In Python, a list is a collection of items of the same or different data type(s). You can loop through the collection to access the items using loop constructs such as for loops.

Like all Python iterables, lists follow zero indexing and support slicing operation.

They are mutable collections so you can modify them in place. This includes adding and removing elements from the list, sorting the elements in a particular order, reversing the order of elements, and much more. Python’s built-in list methods help you perform these actions.

Next, let’s look at some useful Python list methods.

Built-In List Methods in Python

In this section, we’ll learn some list methods that will come in handy. We’ll code examples to see these list methods in action.

We’ll use the following pgm_langs list. It is a list of strings containing the names of popular programming languages.

pgm_langs = ['Python','Go','Rust','JavaScript']

Insert List Items With insert()

You may want to insert an element at a particular index. To do this, you can use the insert() method. The insert() list method call takes in:

  • The index at which the element should be inserted, and
  • The element to insert.

Let’s insert ‘Scala’ at index 1 using the index() method.

pgm_langs = ['Python','Go','Rust','JavaScript']
pgm_langs.insert(1,'Scala')
print(pgm_langs)
# Output: ['Python', 'Scala', 'Go', 'Rust', 'JavaScript']

Add Item to List With append()

<img alt="python-list-methods-1
" data-src="https://kirelos.com/wp-content/uploads/2023/02/echo/2-1-1500×844.png" decoding="async" height="400" src="data:image/svg xml,” width=”750″>

Sometimes you may need to add an element to the end of the list. To do this, you can use the append() method.

Let’s add the string ‘Java’ to the end of the list using the append() method.

pgm_langs.append('Java')
print(pgm_langs)
# Output: ['Python', 'Scala', 'Go', 'Rust', 'JavaScript', 'Java']

Add an Iterable With extend()

You know that you can use the append() method to add a single item. But what if you would like to add more than one item to an existing list, say, a list of items? The extend() method provides a concise syntax to do it.

Let’s add the elements of the list more_langs to the pgm_langs list using the extend() method.

more_langs = ['C  ','C#','C']
pgm_langs.extend(more_langs)
print(pgm_langs)
# Output: ['Python', 'Scala', 'Go', 'Rust', 'JavaScript', 'Java', 'C  ', 'C#', 'C']

You can loop through the list of items and use the append() method to add one item at a time. However, this is verbose. And it’s more convenient to use the extend() method instead.

for lang in more_langs:
    pgm_langs.append(lang)

Reverse a List With reverse()

To reverse the order of elements in a list, you can call the reverse() method.

We see that the pgm_langs list has been reversed in place.

pgm_langs.reverse()
print(pgm_langs)
# Output: ['C', 'C#', 'C  ', 'Java', 'JavaScript', 'Rust', 'Go', 'Scala', 'Python']

Sort a List With sort()

<img alt="Sort-a-List-With-sort" data- data-src="https://kirelos.com/wp-content/uploads/2023/02/echo/Sort-a-List-With-sort.png" data- decoding="async" height="324" src="data:image/svg xml,” width=”653″>

You can sort Python list in place by using the sort() method. Since pgm_langs is a list of strings, we see that the sorting occurs in alphabetical order.

pgm_langs.sort()
print(pgm_langs)
# Output: ['C', 'C#', 'C  ', 'Go', 'Java', 'JavaScript', 'Python', 'Rust', 'Scala']

To sort the list in reverse alphabetical order, you can set the reverse parameter to True in the sort() method call.

pgm_langs.sort(reverse=True)
print(pgm_langs)
# Output: ['Scala', 'Rust', 'Python', 'JavaScript', 'Java', 'Go', 'C  ', 'C#', 'C']

Learn more about sorting Python lists.

Make a Shallow Copy With copy()

It may sometimes be helpful to modify a copy of the original list instead of modifying the original list itself. The list method copy() returns a shallow copy of the Python list.

Let us create a shallow copy of the pgm_langs list and call it pgm_langs_copy. And we set the first element in the list to ‘Haskell’, and print it out.

pgm_langs_copy = pgm_langs.copy()
pgm_langs_copy[0]='Haskell'
print(pgm_langs_copy)
# Output: ['Haskell', 'Rust', 'Python', 'JavaScript', 'Java', 'Go', 'C  ', 'C#', 'C']

However, we see that pgm_langs list is not modifed. Therefore, creating a shallow copy and modifying it  does not change the original list.

print(pgm_langs)
# Output: ['Scala', 'Rust', 'Python', 'JavaScript', 'Java', 'Go', 'C  ', 'C#', 'C']

Get Item Count With count()

Sometimes it’s helpful to know the number of times a particular element occurs in a list. The count() method returns the number of times an element occurs in a list.

In the pgm_langs list, all elements occur exactly once. So when we try to get the count of ‘Go’, we get 1, which is correct.

print(pgm_langs.count('Go'))
# Output: 1

 Using the count() method is one of the ways to remove duplicate items from Python lists.

Get Index of an Item With index()

To find the index of an item at a Python list, you can use the index() method. Suppose we’d like to find the index of ‘C#’ in the pgm_langs list. We can use the assert statement to verify that the element that index 7 is ‘C#’.

print(pgm_langs.index('C#'))
# Output: 7
assert pgm_langs[7] == 'C#'

Remove Item at an Index With pop()

Now, let’s look at list method list methods to remove elements from Python lists. The pop() method is used to remove and return an element at a particular index. From the previous code example, we know that ‘C#’ is the language at index 7.

When we call the pop() method on the pgm_langs list with 7 as the index, we see that it returns ‘C#’, the element at index 7, and it also removes it from the list.

print(pgm_langs.pop(7))
# Output: 'C#'
print(pgm_langs)
# Output: ['Scala', 'Rust', 'Python', 'JavaScript', 'Java', 'Go', 'C  ', 'C']

So the pop() method removes and returns an element at the specified index. However, specifying the index is optional. When you do not specify the index, the pop() method removes and returns the last element in the Python list, as shown:

print(pgm_langs.pop())
# Output: 'C'
print(pgm_langs)
# Output: ['Scala', 'Rust', 'Python', 'JavaScript', 'Java', 'Go', 'C  ']

Remove Items With remove()

<img alt="Remove-Items-With-remove" data- data-src="https://kirelos.com/wp-content/uploads/2023/02/echo/Remove-Items-With-remove.png" data- decoding="async" height="363" src="data:image/svg xml,” width=”532″>

Sometimes, you may know which element to remove but not its index. In this case you can use the remove() method—which takes in an element to remove and removes it. Let’s remove ‘Java’ from the pgm_langs list using the remove() method as shown.

pgm_langs.remove('Java')
print(pgm_langs)
# Output: ['Scala', 'Rust', 'Python', 'JavaScript', 'Go', 'C  ']

Remove All Items With clear()

What if you’d like to remove all the items from a Python list? You can loop through the list and remove each element using the remove() method, like this:

for lang in pgm_langs:
    pgm_langs.remove(lang)

But is there a better way? Yes, using the clear() method. We see that calling the clear method on the pgm_langs list removes all the elements, and pgm_langs is now an empty list.

pgm_langs.clear()
print(pgm_langs)
# Output: []

A Summary of Python List Methods

Let’s quickly summarize the various list methods and their syntax:

List Method Syntax Description
insert() list1.insert(index, elt) Inserts elt at index in list1
append() list1.append(elt) Adds elt to the end of list1
extend() list1.extend(list2) Adds elements from list2 to the end of list1
sort() list1.sort() Sorts the list in place
reverse() list1.reverse() Reverses list1 in place
copy() list1.copy() Returns a shallow copy of list1
count() list1.count(elt) Returns the count of elt in list1
index() list1.index(elt) Returns index of elt in list1
pop() list1.pop(index) Removes elt at index and returns it.
remove() list1.remove(elt) Removes elt from list1
clear() list1.clear() Removes all elements from list1

Conclusion

I hope this tutorial helped you understand how to use some of the most common list methods in Python. As a next step, learn about Python tuples and the differences between lists and tuples in Python. 

If you’re learning Python, you can check out this list of beginner-friendly learning resources.