python said : "1, 2, 3, 6, 7, manbo !"

?

-

At the python2.3.2 prompt
if b == 4:
a.remove(b)
else:
print b


0
1
2
3
6
7


Why 5 does not appear ? (this was the source of a deep bug in a 4000+
lines networked program...)

RodrigoB
 
R

Rodrigo Benenson

(corrected the identation)
At the python2.3.2 prompt

...........if b == 4:
................a.remove(b)
...........else:
................print b


0
1
2
3
6
7


Why 5 does not appear ? (this was the source of a deep bug in a 4000+
lines networked program...)

RodrigoB
 
B

Ben Finney

Why 5 does not appear ? (this was the source of a deep bug in a 4000+
lines networked program...)
Curious:
... if ( b == 4 ):
... a.remove(b)
... print ( b, a )
...
(0, [0, 1, 2, 3, 4, 5, 6, 7])
(1, [0, 1, 2, 3, 4, 5, 6, 7])
(2, [0, 1, 2, 3, 4, 5, 6, 7])
(3, [0, 1, 2, 3, 4, 5, 6, 7])
(4, [0, 1, 2, 3, 5, 6, 7])
(6, [0, 1, 2, 3, 5, 6, 7])
(7, [0, 1, 2, 3, 5, 6, 7])

It seems that, having removed the current item in the list, the index of
the "for" loop hasn't changed. The next iteration increments that
index, even though the list itself has become shorter.
 
B

Ben Finney

You are iterating over a mutable sequence while you are mutating it.
That is a big no-no.

Got a pointer to somewhere that says so? Or at least describes the
expected behaviour for iteration over mutable types?
 
E

Erik Max Francis

- said:
Why 5 does not appear ? (this was the source of a deep bug in a 4000+
lines networked program...)

You are iterating over a mutable sequence while you are mutating it.
That is a big no-no.
 
E

Erik Max Francis

Ben said:
Got a pointer to somewhere that says so? Or at least describes the
expected behaviour for iteration over mutable types?

Tutorial, section 4.2:

http://www.python.org/doc/current/tut/node6.html#SECTION006200000000000000000

"It is not safe to modify the sequence being iterated over in the loop
(this can only happen for mutable sequence types, such
as lists). If you need to modify the list you are iterating over (for
example, to duplicate selected items) you must iterate over
a copy."

This is actually not specific to Python; in C++'s Standard Library, for
instance, there are complicated ramifications of modifying a container's
contents while you're actively maintaining and using an iterator over
it.
 
F

Francis Avila

Erik Max Francis wrote in message said:
You are iterating over a mutable sequence while you are mutating it.
That is a big no-no.

Try this instead:

a = [i for i in range(8) if not i == 4]
 
M

Miki Tebeka

Hello Rodrigo,
if b == 4:
a.remove(b)
else:
print b
try:
a = range(8)
for b in list(a): # Iterate over a shallow copy
if b == 4:
a.remove(b)
else:
print b
0
1
2
3
5
6
7

HTH.
Miki
 
E

Edward C. Jones

Erik said:
- wrote:




You are iterating over a mutable sequence while you are mutating it.
That is a big no-no.

This can sometimes be done safely by iterating backwards:

n = len(seq)
for i in range(n-1, -1, -1):
...
 

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
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top