How to remove elements from a Python list?

In this tutorial, you will learn how can remove elements from a Python list.

Variety of removing elements from a Python list

1. Python remove element from list by value

🗑️ Python remove() method

The Python list remove() method allows you to remove an element from a list by value.

list.remove(element)
  • This method takes only one parameter, which is the element you want to remove.
  • This method removes only the first matching element at a time.
  • It doesn’t return any value, it removes the given element from the original list.
  • If the given element does not found in the list, it will throw a ValueError:- x not in list.
>>> a = [24, 'Java', 36, 'Python', 47, 'Java', 'C++']
>>> a.remove('Java') # removing Java from the list
>>> print(a)
[24, 36, 'Python', 47, 'Java', 'C++']
>>>

2. Python list remove by index number

🏹 Python remove() method

With the help of the Python list pop() method, you can remove an element from a list by the index number of the element.

list.pop(index_number)
  • The pop() method takes only one parameter which is the index number of the element you want to delete from a list.
  • The parameter to this method is optional. By default, it is set to -1 which defines the last element of the list.
  • This method returns the removed element. But, if the given index number is not found in the list, it will throw an index error: pop index out of range.
>>> a = [24, 'Java', 36, 'Python', 47, 'Java', 'C++']
>>> a.pop(5)
'Java'
>>> print(a)
[24, 'Java', 36, 'Python', 47, 'C++']
>>>

3. Python remove first and last element from list

If you want to remove only the first or last element from a Python list, you can do so with the help of the pop() method.

  • Remove first element: Pass 0 in the parameter to the pop() method.
  • Remove last element: Call the pop() method without passing parameter.
>>> a = [24, 'Java', 36, 'Python', 47, 'Java', 'C++']
>>> a.pop(0) # removing first element
24
>>> print(a)
['Java', 36, 'Python', 47, 'Java', 'C++']
>>> 
>>> a.pop() # removing last element
'C++'
>>> print(a)
['Java', 36, 'Python', 47, 'Java']
>>>

4. Python list remove multiple elements by value

a = [35, 'Java', 'Python', 52, 'PHP', 'C++']

def remove_el(items:list):
    for i in items:
        a.remove(i)

remove_el(['PHP', 'C++'])
print(a)
[35, 'Java', 'Python', 52]

5. Python list remove multiple elements by index number

In the following code, remove_el_by_index() function is created to remove multiple elements from a Python list by index number.

This function takes two parameters, one is the targeted list from where you want to remove the elements, and the second is a list of the index numbers you want to delete.

The following function does not return anything it updates the original list.

a = [35, 'Java', 'Python', 52, 'PHP', 'C++']

def remove_el_by_index(target, index_list):
    for i, index in enumerate(index_list):
        target.pop(index - i)

remove_el_by_index(a, [0,3])
print(a)
['Java', 'Python', 'PHP', 'C++']

If you want to return a new list, you can use the following code.

a = [35, 'Java', 'Python', 52, 'PHP', 'C++']

def remove_el_by_index(target, index_list):
    tempList = []
    for i, item in enumerate(target):
        if i not in index_list:
            tempList.append(item)
    return tempList

newList = remove_el_by_index(a, [0,3])
print(newList)
['Java', 'Python', 'PHP', 'C++']

Leave a Reply

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