Removing items from a list

Chris Angelico rosuav at gmail.com
Fri Feb 10 16:58:55 EST 2012


On Sat, Feb 11, 2012 at 7:04 AM, Thomas Philips <tkpmep at gmail.com> wrote:
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>> for i in x:
>        if i % 2 == 0:
>                x.remove(i)

Just a quickie, is there a reason you can't use a list comprehension?

x = [i for i in x if i % 2]

ChrisA



More information about the Python-list mailing list