473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

-
At the python2.3.2 prompt
a = range(8)
for b in a:

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

Jul 18 '05 #1
8 1210
(corrected the identation)
At the python2.3.2 prompt
a = range(8)
for b in a:

...........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

Jul 18 '05 #2
On Wed, 21 Jan 2004 17:53:19 -0300, - wrote:
Why 5 does not appear ? (this was the source of a deep bug in a 4000+
lines networked program...)


Curious:
a = range(8)
for b in a:

... 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.

--
\ "You know I could rent you out as a decoy for duck hunters?" |
`\ -- Groucho Marx |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3
- wrote:
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.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Life is painting a picture, not doing a sum.
-- Oliver Wendell Holmes, Jr.
Jul 18 '05 #4
On Wed, 21 Jan 2004 19:03:44 -0800, Erik Max Francis wrote:
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?

--
\ "Love is the triumph of imagination over intelligence." -- |
`\ Henry L. Mencken |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #5
Ben Finney wrote:
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/tu...00000000000000

"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.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Isn't the best defense always a good attack?
-- Ovid
Jul 18 '05 #6

Erik Max Francis wrote in message <40***************@alcyone.com>...
- wrote:
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.


Try this instead:

a = [i for i in range(8) if not i == 4]
--
Francis Avila
Jul 18 '05 #7
Hello Rodrigo,
a = range(8)
for b in a:

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
Jul 18 '05 #8
Erik Max Francis wrote:
- wrote:

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.


This can sometimes be done safely by iterating backwards:

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

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.