Alter list items within loop

B

Brendan

Can someone please explain what is happening in the output below? The
number 3 never gets printed. Does Python make a copy of a list before
it iterates through it?: print i
if i == 2 :
e.remove(i)


1
2
4[1, 3, 4]
 
E

Emile van Sebille

On 6/11/2009 11:54 AM Brendan said...
Can someone please explain what is happening in the output below?

you delete e[2] before displaying it.

The
number 3 never gets printed. Does Python make a copy of a list before
it iterates through it?:

No.

Mods to a list while passing it is generally not a good idea. Sometimes
passing the list backwards works.

Emile

print i
if i == 2 :
e.remove(i)


1
2
4[1, 3, 4]
 
T

Tim Harig

Can someone please explain what is happening in the output below? The
number 3 never gets printed. Does Python make a copy of a list before
it iterates through it?:

You can see what is happening by printing the list as you work through the
loop:
.... print e
.... print i
.... if i == 2 :
.... e.remove(i)
....
[1, 2, 3, 4]
1
[1, 2, 3, 4]
2
[1, 3, 4]
4

first loop:
i = 0
e = e[0] = 1

second loop
i = 1
e = e[1] = 2

third loop
i = 2
e = e[2] = 4
number 3 never gets printed. Does Python make a copy of a list before
it iterates through it?:

No, complex types are passed by reference unless explicity copied. You can
do what you want by making an explicit copy before entering the loop:
.... print e
.... print i
.... if i == 2 :
.... e.remove(i)
....
[1, 2, 3, 4]
1
[1, 2, 3, 4]
2
[1, 3, 4]
3
[1, 3, 4]
4
 
T

Tim Harig

*All* types are passed by reference unless explicitly copied. Python does
make special cases for simple and complex types.

That is technically true; but, you will not have this issue with simple
singlular data types. Technically the difference as to whether you will
have this problem depends on whether or not an object is mutable.
Simple objects (numbers and strings) are all immutable. Since this issue
revolves around changing objects in place, it cannot arise with immutable
objects. I am not always conscous of whether I am working with objects
that are mutable or immutable; but, I am generally concious of the general
complexity of the object. Whenever I am working with objects that are
complex, I am reminded to watch out for mutability issues. So, while it is not
totally correct to think of it this way, I find it an easier guideline to
follow.
 
L

Lie Ryan

Tim said:
That is technically true; but, you will not have this issue with simple
singlular data types. Technically the difference as to whether you will
have this problem depends on whether or not an object is mutable.
Simple objects (numbers and strings) are all immutable. Since this issue
revolves around changing objects in place, it cannot arise with immutable
objects. I am not always conscous of whether I am working with objects
that are mutable or immutable; but, I am generally concious of the general
complexity of the object. Whenever I am working with objects that are
complex, I am reminded to watch out for mutability issues. So, while it is not
totally correct to think of it this way, I find it an easier guideline to
follow.

Everything is passed as an object[1], no matter how simple or complex,
mutable or immutable.

This can be seen:
.... print id(a)
....8578336

[1] internally as PyObject pointer but that's implementation detail
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top