How to Remove an Element from a List by Index in Python?

How to Remove an Element from a List by Index in Python?

Answer: To remove an element from a list by index in Python you can use the pop() method to remove a single element at once or slice using the del statement to remove multiple elements from a list.

Removing an element from a list is a common operation in Python. Python provides multiple methods to remove single and multiple elements from the list. In this blog let’s explore the different methods to remove the element with the use cases.

Table of Contents:

Methods to Remove an Element from a List by Index in Python

Python provides various methods to remove an element from a list by index. Following are some of these methods explained with the help of examples:

Method 1: Using the pop() function in Python

The pop() function is the most commonly used technique to remove a single element from a list by specifying the index of the element.

Example: Using the pop() function to remove the element from the list

Python

Output:

Explanation:

The pop() method removes the element from the list in the specified index. Only one index position can be specified once to remove multiple elements from the list the pop() method has to be passed multiple times.

Method 2: Using the del statement in Python

The del statement is useful to remove multiple elements from the list at once by specifying the index positions of the elements in the list. 

Example: Using the del statement remove an element from the list

Python

Output:

Explanation:

The del statement removes an element from a list by its index without returning the removed value. In this example, del list[3] removes the element at index 3, which is “blog”.

Method 3: Using the remove() function in Python

The remove() function in Python removes the first occurrence of a specified item from a list. If the item isn’t found, it raises a value error.

Example:

Python

Output:

Explanation:
This code removes element 90 from the list by accessing it using list[2] and passing it to the remove() method. After removal, the list is printed and becomes [30, 60, 120, 150, 180].

Method 4: Using the filter() function in Python

The filter() function creates a new list by keeping only the elements that match a condition. It removes the element you want by excluding it from the new list.

Example:

Python

Output:

Explanation:

The code uses filter() to create a new list by excluding the element at index 0. The result is a new list [‘Learn’, ‘Python’, ‘at’ ‘Intellipaat’] without the element at index 0.

Method 5: Using list comprehension in Python

List comprehension provides a concise way to filter elements based on conditions. In this method, we exclude the element at the specified index while rebuilding the list.

Example:

Python

Output:

Explanation:

This code uses list comprehension with enumerate() to create a new list, excluding the element at index 2 (which is 300). The resulting list is [100, 200, 400, 500].

Conclusion

Each method serves different purposes for removing elements by index in a list. Depending on your requirements, you can use pop() for single-element removal, ‘del’ for multiple or single deletions, or list comprehension and filter() for more flexible or condition-based removal. Understanding these methods helps you remove the element from the list by indexing in Python effectively.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner