Removing an item from a list in Python (clear, pop, remove, del)

by Alex
Removing an item from a list in Python (clear, pop, remove, del)

The List data type in Python helps you store different types of data in a specific linked sequence. Data is written in square brackets and separated by commas. Python has several methods for removing items from a list: remove(), pop(), and clear(). In addition to these there is also the del keyword. Let’s look at all of them.

List example:


my_list = ['Python', 50, 11.50, 'Alex', 50, ['A', 'B', 'C']]

The index starts at 0. In my_list, the line “Python” is at position 0. Next:

  1. Integer 50
  2. Floating point number 11.50
  3. The string again is “Alex”
  4. Another number 50
  5. A list of strings “A”, “B” and “C”

Remove() method

The remove() method is a built-in method that removes the first matching item from the list.

Syntax: list.remove(element)

. It passes the element to be removed from the list. The method does not return values.

How to use:

  • If there are duplicate items in the list, the first matching item will be removed.
  • If there is no element, an error will be thrown with the message that the element is not found.
  • The method does not return values.
  • You must pass a valid value as an argument.

Example: using the remove() method to remove an item from a list

This list has strings and integers. There are repeating items: the string “Mars” and the number 12.


my_list = [12, 'USA', 'Sun', 14, 'Mars', 12, 'Mars']
my_list.remove(12) # remove element 12 at the beginning
print(my_list)
my_list.remove('Mars') # remove the first Mars from the list
print(my_list)
my_list.remove(100) # error
print(my_list)

Output:

['USA', 'Sun', 14, 'Mars', 12, 'Mars']
['USA', 'Sun', 14, 12, 'Mars']
Traceback (most recent call last):
File "wb.py", line 6, in
my_list.remove(100) # error
ValueError: list.remove(x): x not in list

Pop() method

This method removes an element based on the passed index.

Syntax: list.pop(index)

. It takes only one argument – the index.

  • To remove a list item, you need to pass its index. Indexes in lists start at 0. To get the first one, pass 0. To remove the last one, pass -1.
  • This argument is optional. The default value is -1, so by default the last item will be deleted.
  • If that index is not found or is out of range, the method will throw an IndexError: pop index exception.

Returns the element removed from the list by the index. The list itself is updated and no longer contains that item.

Example: Using pop() to remove an item

Let’s try to remove an item with pop:

  • By index
  • By not passing the index
  • By passing the index out of range

Let’s remove “Sun” from the list. The index starts at 0, so the index for “Sun” will be 2.


my_list = [12, 'USA', 'Sun', 14, 'Mars', 12, 'Mars']
# Passing the index as 2 to remove Sun
name = my_list.pop(2)
print(name)
print(my_list)
# pop() method without index - returns last item
item = my_list.pop()
print(item)
print(my_list)
# transfer the index outside the list
item = my_list.pop(15)
print(item)
print(my_list)

Conclusion:

Sun
[12, 'USA', 14, 'Mars', 12, 'Mars']
Mars
[12, 'USA', 14, 'Mars', 12]
Traceback (most recent call last):
File "wb.py", line 14, in
item = my_list.pop(15)
IndexError: pop index out of range

The clear() method

The clear() method removes all items from the list.

Syntax: list.clear()

. There are no parameters or return value.

Example

The clear() method clears the given list. Let’s see:


my_list = [12, 'USA', 'Sun', 14, 'Mars', 12, 'Mars']
element = my_list.clear()
print(element)
print(my_list)

Output:

None
[]

Del keyword

To remove an item from a list you can use the del keyword with the name of the list after it. You will also need to pass the index of the item you want to remove.

Syntax: del list[index]

. It is also possible to select items within a certain range and remove them with del. To do this, you need to pass the start and end values of the range.

Syntax: del list[start:stop]

. Here is an example of how del can be used to remove the first, last, and multiple list entries at once:


my_list = list(range(7))
print("Source list", my_list)
# To remove the first entry
del my_list[0]
print("After removing the first item", my_list)
# To remove an item by index
del my_list[5]
print("After deleting item", my_list)
# To remove multiple items
del my_list[1:5]
print("After deleting several elements", my_list)

Output:

Initial list [0, 1, 2, 3, 4, 5, 6]
After removing the first element [1, 2, 3, 4, 5, 6]
After deleting an element [1, 2, 3, 4, 5]
After deleting several elements [1]

How to remove the first element of a list

The remove(), pop() methods can be used for this. In the case of remove you need to pass the index of the first item, i.e. 0. You can also use the keyword del. The example shows how to use all of these methods.


my_list = ['A', 'B', 'C', 'D', 'E', 'F']
print("List", my_list)
my_list.remove('A')
print("C using remove()", my_list)
my_list = ['A', 'B', 'C', 'D', 'E', 'F']
my_list.pop(0)
print("Using pop()", my_list)
my_list = ['A', 'B', 'C', 'D', 'E', 'F']
del my_list[0]
print("Using del", my_list)

Conclusion:

List ['A', 'B', 'C', 'D', 'E', 'F']
Using remove() ['B', 'C', 'D', 'E', 'F']
Using pop() ['B', 'C', 'D', 'E', 'F']
Using del ['B', 'C', 'D', 'E', 'F']

How to remove multiple items from a list

The remove() and pop() methods can only remove one item. The del method is used to remove multiple items. For example, from the list ['A', 'B', 'C', 'D', 'E', 'F'] you need to remove elements B, C and D. This is how to do it with del.


my_list2 = ['A', 'B', 'C', 'D', 'E', 'F']
print("list", my_list2)
del my_list2[1:4]
print("Using del", my_list2)

Output:

List ['A', 'B', 'C', 'D', 'E', 'F']
Using del ['A', 'E', 'F']

How to remove an item from a list using an index in Python

To remove an item by index, use pop(). The del keyword also works for this.


# Using del to remove multiple items from the list
my_list1 = ['A', 'B', 'C', 'D', 'E', 'F']
print("list", my_list1)
element = my_list1.pop(2)
print("Using pop", my_list1)
# Using del to remove multiple elements from the list
my_list2 = ['A', 'B', 'C', 'D', 'E', 'F']
del my_list2[2]
print("Using del", my_list2)

Conclusion:

List ['A', 'B', 'C', 'D', 'E', 'F']
Using pop ['A', 'B', 'D', 'E', 'F']
Using del ['A', 'B', 'D', 'E', 'F']

Conclusion

There are many ways to remove data from a list in Python. These are the remove(), pop(), clear(), and del keyword methods.

  • remove() – removes the first encountered element in the list that matches the condition.
  • pop() – removes element by index.
  • clear() – removes all items from the list.

Related Posts

LEAVE A COMMENT