Saturday, 17 March 2018

Delete item in a list in Python

Use the list.remove() method to remove the first occurrence, in-place:


a.remove((1, 2))

Demo:


>>> a = [(4, 5), (1, 2), (7, 5), (1, 2), (5, 2)]
>>> a.remove((1, 2))
>>> a
[(4, 5), (7, 5), (1, 2), (5, 2)]

See the Mutable Sequence Types documentation:



s.remove(x)
same as del s[s.index(x)]



and s.index() only ever finds the first occurrence.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...