How to remove the first occurrence of an item in a list in Python

How to remove the first occurrence of an item in a list in Python:

This post will show you how to remove the first occurrence of an item from a list in Python. The program will use one predefined list and one number to remove from the list. There are different ways to solve this problem in Python. I will show you four different ways.

Method 1: By using the remove() method:

The remove() method of Python list removes the first occurrence of an element from a list. This method takes the element as its parameter and removes its first instance. The syntax of the remove() method is:

list.remove(e)
  • e is the element to remove. If the element doesn’t exists in the list, it throws a ValueError.
  • The return value of this method is None or it returns nothing.

Let’s take a look at the following example:

days = ['sun', 'mon', 'tues', 'thurs', 'fri', 'sat']

print(f'Given list: {days}')

days.remove('tues')

print(f'Final list: {days}')
  • It has a predefined list of strings days.
  • It uses the remove() method to remove the string from the list.
  • It prints the content of the list before and after the string is removed.

If you run this program, it will print:

Given list: ['sun', 'mon', 'tues', 'thurs', 'fri', 'sat']
Final list: ['sun', 'mon', 'thurs', 'fri', 'sat']

The string 'tues' is removed from the list.

If the list has multiple instances of the element we are trying to remove, it will remove the first occurrence of the element. Let’s consider the following example:

days = ['sun', 'mon', 'tues', 'thurs', 'fri', 'tues', 'sat']

print(f'Given list: {days}')

days.remove('tues')

print(f'Final list: {days}')

The list days includes two instances of the string 'tues'. The remove() method will remove the first one from the list. If you run this program, it will print:

Given list: ['sun', 'mon', 'tues', 'thurs', 'fri', 'tues', 'sat']
Final list: ['sun', 'mon', 'thurs', 'fri', 'tues', 'sat']

Method 2: By using the pop() method:

The pop method is another method to remove Python list elements. This method takes the index of the element to remove as its parameter. The syntax of the pop method is:

list.pop(i)
  • The i is the only parameter requires. It is the index of the element to remove from the list.
  • The parameter i is optional. If we don’t provide its value, it will remove the last element of the list, i.e. -1 is passed as the parameter.
  • The pop method supports negative indexing. e.g. if we pass -1, it will remove the last element, -2 will remove the second last element etc.
  • The pop method returns the deleted item.
  • For invalid index value, it throws IndexError: pop index out of range exception.

We can use the index() method to get the index of the first occurrence of an element. Note that this method throws ValueError if the element is not found in the list.

days = ['sun', 'mon', 'tues', 'thurs', 'fri', 'tues', 'sat']

print(f'Given list: {days}')

days.pop(days.index('tues'))

print(f'Final list: {days}')

It will print:

Given list: ['sun', 'mon', 'tues', 'thurs', 'fri', 'tues', 'sat']
Final list: ['sun', 'mon', 'thurs', 'fri', 'tues', 'sat']

Method 3: By using the del operator:

The del operator is similar to the pop() method. The only difference is that it doesn’t return the deleted item. The following program shows how we can use the del operator to remove an item from a list:

days = ['sun', 'mon', 'tues', 'thurs', 'fri', 'tues', 'sat']

print(f'Given list: {days}')

del days[days.index('tues')]

print(f'Final list: {days}')

It will print the same output.

You might also like: