note.nkmk.me

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

Posted: 2019-05-29 / Modified: 2020-01-28 / Tags: Python, List

In Python, list's methods clear(), pop(), and remove() are used to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

  • Remove all items: clear()
  • Remove an item by index and get its value: pop()
  • Remove an item by value: remove()
  • Remove items by index or slice: del
  • Remove multiple items that meet the condition: List comprehensions

See the following post for adding items to the list.

Sponsored Link

Remove all items: clear()

You can remove all items from the list with clear().

l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

l.clear()
print(l)
# []

Remove an item by index and get its value: pop()

You can remove the item at the specified position and get the value of that item with pop().

The index at the beginning is 0 (zero-based indexing).

l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(l.pop(0))
# 0

print(l)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

print(l.pop(3))
# 4

print(l)
# [1, 2, 3, 5, 6, 7, 8, 9]

You can use negative values to specify the position from the end. The index at the end is -1.

print(l.pop(-2))
# 8

print(l)
# [1, 2, 3, 5, 6, 7, 9]

If the argument is omitted, the last item is deleted.

print(l.pop())
# 9

print(l)
# [1, 2, 3, 5, 6, 7]

Specifying a nonexistent index raises an error.

# print(l.pop(100))
# IndexError: pop index out of range

Remove an item by value: remove()

You can remove the first item from the list where its value is equal to the specified value with remove().

l = ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']
print(l)
# ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']

l.remove('Alice')
print(l)
# ['Bob', 'Charlie', 'Bob', 'Dave']

If the list contains more than one matching the specified value, only the first one is deleted.

l.remove('Bob')
print(l)
# ['Charlie', 'Bob', 'Dave']

Specifying a nonexistent value raises an error.

# l.remove('xxx')
# ValueError: list.remove(x): x not in list
Sponsored Link

Remove items by index or slice: del

clear(), pop() and remove() are methods of list. You can also remove elements from a list with del statements.

Specify the item to be deleted by index. The first index is 0, and the last index is -1.

l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

del l[0]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

del l[-1]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8]

del l[6]
print(l)
# [1, 2, 3, 4, 5, 6, 8]

Using slice, you can delete multiple items at once.

l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

del l[2:5]
print(l)
# [0, 1, 5, 6, 7, 8, 9]

l = list(range(10))
del l[:3]
print(l)
# [3, 4, 5, 6, 7, 8, 9]

l = list(range(10))
del l[4:]
print(l)
# [0, 1, 2, 3]

l = list(range(10))
del l[-3:]
print(l)
# [0, 1, 2, 3, 4, 5, 6]

It is also possible to delete all items by specifying the entire range.

l = list(range(10))
del l[:]
print(l)
# []

You can also specify step as [start:stop:step].

l = list(range(10))
del l[2:8:2]
print(l)
# [0, 1, 3, 5, 7, 8, 9]

l = list(range(10))
del l[::3]
print(l)
# [1, 2, 4, 5, 7, 8]

See the following article for details on slices.

Remove multiple items that meet the condition: List comprehensions

Removing items that satisfy the condition is equivalent extracting items that do not satisfy the condition.

For this purpose, list comprehensions are used.

An example of removing odd or even items (= keeping even or odd items) is as follows. % Is the remainder operator and i % 2 is the remainder of dividing i by 2.

In the list comprehension, a new list is generated. Unlike the list type method or del statement introduced so far, the original list is not changed.

l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print([i for i in l if i % 2 == 0])
# [0, 2, 4, 6, 8]

print([i for i in l if i % 2 != 0])
# [1, 3, 5, 7, 9]

print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Other examples are as follows.

l = ['Alice', 'Bob', 'Charlie', 'Bob', 'David']
print(l)
# ['Alice', 'Bob', 'Charlie', 'Bob', 'David']

print([s for s in l if s != 'Bob'])
# ['Alice', 'Charlie', 'David']

print([s for s in l if s.endswith('e')])
# ['Alice', 'Charlie']
Sponsored Link
Share

Related Categories

Related Posts