Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20193

Re: Removing items from a list

References <7c4cc084-7b55-48f9-a3ac-219f33b69d0b@k10g2000yqk.googlegroups.com>
Date 2012-02-11 08:58 +1100
Subject Re: Removing items from a list
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5680.1328911138.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Feb 11, 2012 at 7:04 AM, Thomas Philips <tkpmep@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-10 12:04 -0800
  Re: Removing items from a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-10 13:22 -0700
    Re: Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-10 12:48 -0800
  Re: Removing items from a list MRAB <python@mrabarnett.plus.com> - 2012-02-10 20:26 +0000
  Re: Removing items from a list Chris Angelico <rosuav@gmail.com> - 2012-02-11 08:58 +1100
    Re: Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-13 05:55 -0800

csiph-web