Remove Index from List in Python (2 Examples)

In this tutorial, you’ll learn how to remove an element from a list by its index in Python. Removing an element by a certain index is a common operation when working with lists, as it allows you to selectively delete specific elements. We’ll explore different examples of removing an element from a list by index.

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 = ['Car', 'Bike', 'Plane', 'Boat', 'Helicopter', 'Scooter']

This creates a list, my_list, with several elements.

Example 1: Remove an Index from a List Using the del Keyword

One way to remove an element from a list by its index is using the del keyword. Here’s an example:

# Remove element using the del keyword
index = 2
del my_list[index]
 
# Print the updated list
print(my_list)
# ['Car', 'Bike', 'Boat', 'Helicopter', 'Scooter']

In this example, we specify the index of the element we want to remove using the variable index. We then use the del keyword followed by the list name and the index to delete the element at that specific index.

After removing the element, the list is updated, and we print the updated list to verify the change.

Example 2: Remove an Index from a List Using the pop() Method

Another method to remove a specific index value from a list is using the pop() method. Here’s an example:

# Remove element using the pop() method
index = 4
removed_element = my_list.pop(index)
 
# Print the updated list and the removed element
print(my_list)
# ['Car', 'Bike', 'Boat', 'Helicopter']
print(removed_element)
# Scooter

In this example, we specify the index of the element we want to remove using the variable index. We call the pop() method on the list, passing the index as an argument.

The pop() method removes and returns the element at the specified index. We can assign the removed element to a variable, like removed_element, if we need to use it later.

After removing the element, the list is updated, and we print the updated list and the removed element to verify the changes.

Video, Further Resources & Summary

In this tutorial, we explored different examples of removing an element from a list by its index in Python. We learned how to use the del keyword and the pop() method to remove elements at specific positions in a list. Each method provides a different approach and flexibility depending on your specific needs.

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 Python programming tutorials and examples, you can check out the following resources on Statistics Globe:

Now you have the knowledge and techniques to remove an element from a list by its index in Python. Happy coding!

Ömer Ekiz Python Programming & Informatics

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top