Remove an element from a Python List

This tutorial will discuss how to remove an element from a Python list.

In Python, the list class provides a method named remove() that accepts a value as an argument and removes the first occurrence of that value from the list. It’s important to note that it doesn’t remove all occurrences of the given value, just the first one. Additionally, if the given value doesn’t exist in the list, the remove() method will raise a ValueError.

With these points in mind, we’ve created a custom function that accepts both a list and a value as arguments.

def removeElement(listObj, value):
    # Attempt to remove the value if it's in the list
    while value in listObj:
        listObj.remove(value)
    return listObj

This function will remove all occurrences of the specified value from the list and return the modified list. If the given value doesn’t exist in the list, the function will return the same list unchanged and will print a message on the console. However, it won’t raise any errors. We manage potential errors within the function itself.

Here’s how our function works: Suppose we have a list of integers and we want to remove all occurrences of the value 3. Calling our function with the list and 3 as arguments will remove all instances of 3.

# Value of the element that needs
# to be removed from list
val = 3

# Remove element with value 3 from the List
sampleList = removeElement(sampleList, val)

# List after removing value 3: [45, 67, 22, 45, 22, 89, 71, 22, 51, 89, 89, 11]

If the value 3 doesn’t exist in the list, the function will return the original list unchanged and have no effect on our list object.

Let’s look at another example where we want to remove all occurrences of the value 89.

# Value of the element that needs
# to be removed from list
val = 89

# Remove element with value 89 from the List (first occurrence)
sampleList = removeElement(sampleList, val)

# List after removing value 89: [45, 67, 22, 45, 22, 71, 22, 51, 11]

By passing the list and the value 89 as arguments to our custom “remove element” function, it will remove all instances of 89 and return the modified list.

Let’s see the complete example,

def removeElement(listObj, value):
    # Attempt to remove the value if it's in the list
    while value in listObj:
        listObj.remove(value)
    return listObj

# A List of Numbers
sampleList = [45, 67, 22, 45, 22, 89, 71, 22, 51, 89, 89, 11]
print("Original List:", sampleList)

# Value of the element that needs
# to be removed from list
val = 3

# Remove element with value 3 from the List
sampleList = removeElement(sampleList, val)

print(f"List after removing value {val}:", sampleList)


# Value of the element that needs
# to be removed from list
val = 89

# Remove element with value 89 from the List (first occurrence)
sampleList = removeElement(sampleList, val)

print(f"List after removing value {val}:", sampleList)

Output

Original List: [45, 67, 22, 45, 22, 89, 71, 22, 51, 89, 89, 11]
List after removing value 3: [45, 67, 22, 45, 22, 89, 71, 22, 51, 89, 89, 11]
List after removing value 89: [45, 67, 22, 45, 22, 71, 22, 51, 11]

Summary

Today, we learned how to remove an element from a Python list.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top