Complete Guide to Removing Elements From Lists in Python
While lists aren’t the most efficient data structure if you’ll be doing lots of deleting from the middle, there are definitely good ways to accomplish the task. The built-in remove() method should be your first option. Let’s go over some examples.
Remove element in Python list by value
primes = [2, 3, 5, 5, 7, 11]
primes.remove(5)
print(primes)
# [2, 3, 5, 7, 11]
primes.remove(5)
# [2, 3, 7, 11]
primes.remove(5)
# careful, this will throw an error
# ValueError: list.remove(x): x not in list
If you want to safely remove items, and you aren’t sure if they exist in the list or not, you can either catch the error:
try:
primes.remove(5)
except Exception as e:
print("not in list")
Or, you can check for existence first:
if 5 in primes:
primes.remove(5)
Remove an element in Python list by index
The del statement is a built-in keyword that allows you to remove items from lists. The simplest example deletes the item at a given index.
primes = [2, 3, 5, 5, 7, 11]
# delete the second item
del primes[1]
print(primes)
# [2, 5, 5, 7, 11]
Again, you need to be careful. If the index doesn’t exist an error will be raised.
primes = [2, 3, 5, 5, 7, 11]
# delete the eleventh item
del primes[10]
IndexError: list assignment index out of range
if len(primes) >= 10:
del primes[10]
Remove multiple of items from a python list
primes = [2, 3, 5, 5, 7, 11]
# deleting items from 2nd to 4th
del primes[1:4]
print(primes)
# [2, 7, 11]
Remove item by index and return it
The .pop() method removes an item from a list by index and returns that item.
primes = [2, 3, 5, 7]
# pop the second element
popped = primes.pop(1)
print("popped:", popped)
# 3
print("list:", primes)
# [2, 5, 7]
If you don’t pass an index parameter to pop() it will default to -1 and remove the last element from the list. Just like the other methods, if you pass in a number too large, you’ll get the following error.
IndexError: pop index out of range
Related Articles
How to Use the Ternary Operator in Python
Dec 09, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
Developers love concise code that’s easy to read, and that’s exactly what ternary operators are for. The ternary operator in Python lets you perform a small if/else statement in a single line. Let’s take a look at a few examples.
Removing Duplicates From a List in Python
Dec 09, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
Let’s go over a few idiomatic ways to remove duplicates from lists in Python. Method #1 - Create a new list (simplest) This is the easiest algorithm to code, but because it requires creating a new list, also requires more memory and is a bit slower.
How to Check if a File Exists in Python
Dec 08, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
When working with files in Python, you’ll often need to check if a file exists before you do anything else with it, such as reading from or writing to it. Luckily, the Python standard library makes this a piece of cake.
Python vs C++: The Best Language To Learn For You
Nov 17, 2021 by Meghan Reichenbach
It’s either a blessing or a curse when choosing to learn Python or C++ because there couldn’t be two more opposing languages to compare.