Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Development Last updated: September 5, 2023
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

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']
# Output: ['Python', 'Scala', 'Go', 'Rust', 'JavaScript']

Add Item to List With append()


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.

# 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']
# 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:

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.

# Output: ['C', 'C#', 'C++', 'Java', 'JavaScript', 'Rust', 'Go', 'Scala', 'Python']

You may also read how to reverse a list in Python to spin it backwards.

Sort a List With sort()


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.

# 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.

# 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()
# 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.

# 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.

# 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#’.

# 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.

# Output: 'C#'
# 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:

# Output: 'C'
# Output: ['Scala', 'Rust', 'Python', 'JavaScript', 'Java', 'Go', 'C++']

Remove Items With remove()


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.

# 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:

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.

# Output: []

A Summary of Python List Methods

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

List MethodSyntaxDescription
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


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. 

  • Bala Priya C
    Bala Priya is a developer and technical writer from India with over three years of experience in the technical content writing space. She shares her learning with the developer community by authoring tech tutorials, how-to guides, and more…. read more
  • Narendra Mohan Mittal

    Narendra Mohan Mittal is a Senior Digital Branding Strategist and Content Editor with over 12 years of versatile experience. He holds an M-Tech (Gold Medalist) and B-Tech (Gold Medalist) in Computer Science & Engineering.

    read more
Thanks to our Sponsors
More great readings on Development
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder