The list is the most versatile core datatype in Python, it represents a mutable collection of ordered items in which items can be accessed, added, removed, and rearranged in a variety arbitrary order.

Lists support removal operations, in which case an element is removed from the original list without creating a new list. This can mostly be attributed to the list's mutable nature which allows operations to be performed on the list in-place.

There are various ways that we can conveniently remove an element from a list:

  • Using pop() method
  • Using remove() method
  • using del statement
  • Creating another list with the element missing.

The first three approaches i.e using  pop(), remove() and del removes an element permanently from a list, in some cases we may, however, want to keep the original list untouched that is why we introduced the fourth approach in which case we create a new list with the element to be removed filtered out.

Using pop() method

Lists contains the pop() method which removes an element from a specified position in the list and returns it. We use this approach when we know the  index of the element to be removed.

mylist.pop(index = -1)

This method removes the element at the specified index from  the list, the index defaults to -1 meaning that if no index is given, the last element will be removed. It returns the just removed element.

 #the list
fruits = ["apple", "banana", "guava", "raspberry", "Orange"]

#remove an element at index 1
fruits.pop(1)
print(fruits)

#remove an element at index 3
fruits.pop(3)

print(fruits) 

The pop() method will raise an IndexError if the specified index is out of range.

mylist =[10, 20, 30]

#raises an IndexError
mylist.pop(6)

Using remove() method

The remove() method takes the item to be removed rather than its index. Since list allow duplicate elements, the remove() methods only removes the leftmost occurrence of an object from the list.

mylist.remove(value)
 countries = ['Japan', 'Russia', 'Canada', 'Russsia']

countries.remove('Russia')

print(countries) 

We can also combine the remove() method with list subscription to remove an element using its index rather than its value.

 countries = ['Japan', 'Russia', 'Canada', 'Russsia']

#remove an object using its index rather than its value
countries.remove(countries[1])

print(countries) 

It is, however, worth noting that the element removed will not necessarily be the one at the specified index but rather the first one to be encountered with a similar value. 

The method raises a ValueError if the value to be removed does not exist in the list.

 countries = ['Japan', 'Russia', 'Canada', 'Russsia']

countries.remove('India') 

Using del statement

The del keyword is used to delete objects by de-referencing them from an associated identifier.

del item

When we use the del statement with a list index, it permanently removes the item at the specified index from the list.

mylist = ['Python', 'Java', 'PHP', 'C++']

del mylist[2]
print(mylist)

del mylist[0]
print(mylist)

 The method will raise an IndexError if the specified index is out of range.

mylist = ['Python', 'Java', 'PHP', 'C++']

del mylist[7]

Creating another list with the element missing.

All the previous approaches altered the original list,   but as earlier mentioned sometimes it may desirable to leave the original list intact and instead create a new list from the elements of the original list but with the element(s) to be removed not included. There are numerous ways to achieve this.

The most concise approach is through list comprehension with a condition to filter out the element to be removed.

mylist = ['Python', 'Java', 'C++', 'PHP', 'Ruby']


#remove the object at index 2
new_list = [v for i, v in enumerate(mylist) if i != 2 ]

print(new_list)

The builtin enumerate()  function iterates over a collection of elements an yields both the index and the element as a tuple. We used it above to keep the track of the current index in the list, this is far more efficient than using the index() methods of the list which would have to traverse through the list  during each iteration.

We can also achieve the same using list comprehension but with the actual value specified rather than its index as shown below.

mylist = ['Python', 'Java', 'C++', 'PHP', 'Ruby']

val = 'Java'
#remove the object at index 2
new_list = [v for v in mylist if v != val ]

print(new_list)

The above approach will remove all the occurrences of the given value from the new list. 

Another way to achieve this is by using the builtin filter() function to filter out the target value from the new list.

filter(function, iterable)

The function argument defines the criteria for inclusion or exclusion, and the iterable is the collection of values being filtered.

mylist = ['Python', 'Java', 'Ruby', 'C++']
val = 'C++'

new_list = list(filter(lambda x: x!= val, mylist))

print(new_list)