[FRPythoneers] using pop() on a list

Erick Bodine erick at allocity.com
Thu Jan 2 10:45:01 MST 2003


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
immediately following the pop but continues w/ the remainder of the
items in the list.  Any ideas on how to get around this, any help would
be much appreciated (See code example below)

x = [1,2,3,4,5,6]
i = 0
for w in x:
 	print "Before: ", w
 	if w == 4:
 		h = x.pop(i)
 		print "Popped", h
 		print "After pop: ", w
 	print "After: ", w
 	i += 1
 	
Before:  1
After:  1
Before:  2
After:  2
Before:  3
After:  3
Before:  4
Popped 4
After pop:  4
After:  4
Before:  6
After:  6

** Where did list item '5' go??

--ERick



More information about the FRPythoneers mailing list