473,320 Members | 1,933 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.

Bug with lists of pairs of lists and append()

Well,

could some kind soul please explain to me why the following trivial code is
misbehaving?

#!/usr/bin/python

lst = [ 0, 1, 2 ]

s = []

l = [ lst[0] ]
r = lst[1:]
while r:
x = (l,r)
print x
s.append( x )

l.append( r.pop(0) )

print s

The output I get is:

([0], [1, 2])
([0, 1], [2])
[([0, 1, 2], []), ([0, 1, 2], [])]

and the error is in the last line: the two pairs in the outer list are
identical and they should be as the pairs on the first and the 2nd line,
respectively!

I think I'm going nuts -- for the life of me I don't see what's going on ...
(I've been tracking down a bug in my larger python script, and the cause
seems to boil down to the above snippet.)

Thanks a lot in advance for any insights, etc.

Best regards,
Gabriel.

Sep 28 '07 #1
3 1690
On 9/28/07, Gabriel Zachmann <za**@removeme.in.tu-clausthal.dewrote:
Well,

could some kind soul please explain to me why the following trivial code is
misbehaving?

#!/usr/bin/python

lst = [ 0, 1, 2 ]

s = []

l = [ lst[0] ]
r = lst[1:]
while r:
x = (l,r)
print x
s.append( x )

l.append( r.pop(0) )

print s

The output I get is:

([0], [1, 2])
([0, 1], [2])
[([0, 1, 2], []), ([0, 1, 2], [])]

and the error is in the last line: the two pairs in the outer list are
identical and they should be as the pairs on the first and the 2nd line,
respectively!

I think I'm going nuts -- for the life of me I don't see what's going on ...
(I've been tracking down a bug in my larger python script, and the cause
seems to boil down to the above snippet.)

Thanks a lot in advance for any insights, etc.

Best regards,
Gabriel.

--
http://mail.python.org/mailman/listinfo/python-list
If you're familiar with C or C++, think of s as holding a pointer to x
which in turn holds a pointer to l and r, so when you change l or r, x
(and s indirectly) is still pointing to the same lists which by the
end of your loop have changed to r=[] and l=[0,1,2].

BTW: It's not really "misbehaving." It's doing exactly what you're
telling it to do ;-)

Jason
Sep 28 '07 #2
If you're familiar with C or C++, think of s as holding a pointer to x
which in turn holds a pointer to l and r, so when you change l or r, x
(and s indirectly) is still pointing to the same lists which by the
AH - thanks a million -- that makes it crystal clear!
[Python's apparent simplicity keeps making me forget that everything is
a pointer ...]
BTW: It's not really "misbehaving." It's doing exactly what you're
telling it to do ;-)
i had a feeling ... ;-)

Cheers,
Gabriel.

--
__________________________________________________ ____________
Life is so constructed that the event does not, cannot,
will not match the expectation. (Charlotte Bronte)
__________________________________________________ ____________
zach in.tu-clausthal.de __@/' www.gabrielzachmann.org
__________________________________________________ ____________
Oct 1 '07 #3
x = (list(l), list(r))
BTW: I prefer this syntax, because it makes the copy explicit, while
l[:] seems to me more "implicit" ...

Best regards,
Gabriel.

--
__________________________________________________ ____________
Life is so constructed that the event does not, cannot,
will not match the expectation. (Charlotte Bronte)
__________________________________________________ ____________
zach in.tu-clausthal.de __@/' www.gabrielzachmann.org
__________________________________________________ ____________
Oct 1 '07 #4

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

Similar topics

3
by: Todd MacCulloch | last post by:
Suppose I have a master list and I need to create two derived lists, something like: s0 = s1 = s2 = But suppose generating s0 is expensive and/or s0 is big. In otherwords I'd like to go...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
4
by: Piotr Filip Mieszkowski | last post by:
Hello, I like both C++ and Lisp and sometimes try to mix their ideas in the code I write. Recently I started to think about writing a pair class similar to the CONS in Lisp. (For those of you...
5
by: Jonathan Burd | last post by:
Greetings everyone, I wrote a function to learn about variable-length argument lists. I wonder if there is a better way to detect the end of the argument list than using a sentinel value like...
10
by: Peter Kleiweg | last post by:
This does not what I want it to do: >>> a = ] * 6 >>> a.append('X') >>> a , , , , , ] This does what I want: >>> b = for _ in range(6)]
9
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last...
11
by: rshepard | last post by:
I start with a list of tuples retrieved from a database table. These tuples are extracted and put into individual lists. So I have lists that look like this: . When I concatenate lists, I end up...
2
by: Gabriel Zachmann | last post by:
Well, could some kind soul please explain to me why the following trivial code is misbehaving? #!/usr/bin/python s = l = r =
9
by: Aaron Watters | last post by:
....is to forget they are sorted??? While trying to optimize some NUCULAR libraries I discovered that the best way to merge 2 sorted lists together into a new sorted list is to just append them...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.