Remove Element from List by Index in Python (2 Examples)
In this tutorial, you’ll learn how to remove an element from a list by its index in Python. Removing elements from a list is a common operation when working with data, and it can be done efficiently using the built-in functions and methods available in Python. We’ll explore different approaches to remove elements by index from a list.
The table of contents is structured as follows:
Let’s get started!
Initializing a Sample List
Firstly, let’s initialize a sample list that we’ll be working with throughout the tutorial:
# Initializing a sample list my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']
This creates a list, my_list
, with several elements.
Example 1: Remove an Element from Lists Using the del Statement
One way to remove an element from a list by the index is using the del statement. Here’s an example:
# Remove element by index using the `del` statement index = 2 del my_list[index] # Removes the element at index 2 # Print the updated list print(my_list) # ['apple', 'banana', 'date', 'elderberry']
In this example, we specify the index of the element we want to remove using the index
variable. We use the del
statement followed by the list name and the index within square brackets to remove the element at that index.
The del statement modifies the original list by removing the element. Finally, we print the updated list. Note that the element at index 2, which was originally ‘cherry’, has been removed from the list.
Example 2: Remove an Element from Lists Using the pop() Method
Another approach to removing an element from a list by the index is using the pop() method. Here’s an example:
# Remove element by index using the pop() method index = 1 removed_element = my_list.pop(index) # Removes the element at index 1 # Print the removed element and the updated list print(removed_element) # banana print(my_list) # ['apple', 'date', 'elderberry']
In this example, we specify the index of the element we want to remove using the index
variable. We use the pop()
method on the list and pass the index as an argument.
The pop()
method returns the removed element, which we assign to the removed_element
. The original list is modified by removing the element at the specified index. Finally, we print the removed element and the updated list.
Note that the element at index 1, which was originally ‘banana’, has been removed from the list.
Video, Further Resources & Summary
In this tutorial, we explored different methods to remove an element from a list by the index in Python. Using the del statement or the pop() method, you can efficiently remove elements from a list based on their index.
Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
For more information and examples related to Python programming, you can check out the following tutorials on Statistics Globe:
- Loop Over List with Index in Python (2 Examples)
- How to Use Lists in Python
- Loop Through List of Strings in Python (3 Examples)
- Iterative Bottom Up Merge Sort in Python (Example)
- Merge Sort Counting Inversions in Python (Example)
Now you have the knowledge and techniques to remove elements from a list by index in Python. Happy coding!
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter