list.pop and print doing funny things

L

Larry Bates

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 said:
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
 
G

Gordon Williams

Larry Bates said:
Whenever you pop from the list it is destructive
and the result is returned, which means it will
be echoed in the interpreter.

Forgot about the echo. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top