how to remove elments from a list code example
Example 1: remove element from list
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
Example 2: python how to remove elements from a list
# Basic syntax:
my_list.remove(element)
# Note, .remove(element) removes the first matching element it finds in
# the list.
# Example usage:
animals = ['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig', 'rabbit'] # Note only 1st instance of
# rabbit was removed from the list.
# Note, if you want to remove all instances of an element, convert the
# list to a set and back to a list, and then run .remove(element) E.g.:
animals = list(set['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']))
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig']