Removing list elements by value with remove()

It's also possible to remove elements by value, rather than by position, using the remove() method:

>>> u.remove('jackdaws')
>>> u
['love', 'my', 'sphinx', 'of', 'quartz']

This is equivalent to the more verbose:

>>> del u[u.index('jackdaws')]

Attempting to remove() an item which is not present will also cause a ValueError to be raised:

>>> u.remove('pyramid')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset