How to remove elements from a Python List

Remove elements from a List

The remove method removes the first occurrence of a value from a list. remove method has the following syntax.

alist.remove(x)

It deletes the first occurrence of object x from alist; raises an exception if not found. Same as del alist[alist.index(x)].


x = ['to', 'be', 'or', 'not', 'to', 'be'] 
print x.remove('be') 
print x

The code above generates the following result.

Assignment to slices: Remove some


a = ['spam', 'eggs', 100, 1234]
# Remove some:
a[0:2] = []
print a

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary