How to remove an item from a list in python?

Last Updated On Tuesday 24th Aug 2021

remove item from list python

What is a list?

A list in python is not so different from the standard definition of a list, such as a names list or a to-do list.

  • In python, a list is a data type used to store multiple items (pieces of data) in a particular order (i.e. ‘ordered’).
  • We can also edit lists to change what is in them (i.e. they are ‘mutable’)
  • Just like with primitive data types in python, we can assign and update lists using variables

Removing items

	names = ['Liam', 'Olivia', 'Charlotte', 'Noah']
print(names)

# remove the Olivia
names.remove('Olivia')
print(names)
	
	['Liam', 'Olivia', 'Charlotte', 'Noah']
['Liam', 'Charlotte', 'Noah']
	

We can’t remove an item that doesn’t exist though

	names = ['Liam', 'Olivia', 'Charlotte', 'Noah']

# remove the Charlotte
names.remove('Charlotte')
print(names)
	

So, here we’re trying to remove ‘Charlotte’ from the list, even though it isn’t in the list.

	Traceback (most recent call last):
  File "listsRemove.py", line 4, in <module>
    names.remove('Charlotte')
ValueError: list.remove(x): x not in list