Saturday, March 17, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...