Removing items from a list

Thomas Philips tkpmep at gmail.com
Fri Feb 10 21:48:49 CET 2012


Thanks for the insight. I saw the behavious as soon as I extended x
with a bunch of 0's

>>> x = list(range(10))
>>> x.extend([0]*10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> for i in reversed(x):
	if i % 2 == 0:
		x.remove(i)

>>> x
[1, 3, 5, 7, 9]

>>> x = list(range(10))
>>> x.extend([0]*10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> for i in x:
	if i % 2 == 0:
		x.remove(i)


>>> x
[1, 3, 5, 7, 9, 0, 0, 0, 0, 0]



More information about the Python-list mailing list