list.pop and print doing funny things
Larry Bates
lbates at swamisoft.com
Tue Sep 21 15:08:10 EDT 2004
Whenever you pop from the list it is destructive
and the result is returned, which means it will
be echoed in the interpreter.
I think what you meant was:
for x in range(len(l)):
value=l.pop(0) # From beginning
print value
print l
# or
value=l.pop() # From end
print value print l
or
while l:
print l.pop(0) # From beginning
print l
# or
print l.pop() # From end
print l
The indexing, etc. wasn't required.
Larry Bates
"Gordon Williams" <g_will at cyberus.ca> wrote in message
news:mailman.3669.1095791640.5135.python-list at python.org...
> An easy question....
>
> Why do I get the x printed twice in the sample below? It is like print x
> is
> being executed twice in each loop.
>
>
>>>> l = [1,2,3,4]
>>>> for x in l[:]:
> ... print x
> ... l.pop(l.index(x))
> ... print l
> ...
> 1
> 1
> [2, 3, 4]
> 2
> 2
> [3, 4]
> 3
> 3
> [4]
> 4
> 4
> []
>>>>
>
> Regards,
>
> Gordon Williams
>
More information about the Python-list
mailing list