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.
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.
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.
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.
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#’.
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.
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:
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.
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:
Inserts elt at index in list1
Adds elt to the end of list1
Adds elements from list2 to the end of list1
Sorts the list in place
Reverses list1 in place
Returns a shallow copy of list1
Returns the count of elt in list1
Returns index of elt in list1
Removes elt at index and returns it.
Removes elt from list1
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.
Python is a very versatile language, and Python developers often have to work with a variety of files and get information stored in them for processing. One popular file format you’re bound to encounter as a Python developer is the Portable Document Format popularly known as PDF