[FRPythoneers] using pop() on a list

Sean Reifschneider jafo at tummy.com
Thu Jan 2 10:58:08 MST 2003

On Thu, Jan 02, 2003 at 10:45:01AM -0700, Erick Bodine wrote:
>I am having some trouble w/ using pop on a list.  I want to loop over
>the list checking each item in the list for a condition & the then pop()
>the matching item.  When I do this, the 'for' loop skips the item

One way to do this is to iterate backwards over the list:

   for index in xrange(len(list), 0, -1):
      w = list[index]

That way you aren't trying to touch things after the POP, only before.

Of course, it kind of depends on exactly what your overall goal is, as
to wether there's a better way to do it.

This problem is something that iterators should address, but I don't
know what you have to do to get a list to be iteratorable.

 Tools may limit the user, but the utility of tools is limited by the
 skill of the user.  -- Leonard Compagno
Sean Reifschneider, Inimitably Superfluous <jafo at tummy.com>
tummy.com, ltd. - Linux Consulting since 1995.  Qmail, Python, SysAdmin

More information about the FRPythoneers mailing list