You Are Here:

Community: Developer Discussion Boards

#1 Old Question Common python question about lists - 2009-01-22, 16:21

Join Date: Oct 2006
Posts: 30
Location: Austria - Vienna
andipro
Offline
Registered User
I was very surprised about the result of following code:
Code:
list0 = [u"Zero",u"One",u"Two"]
list1 = list0
list1.insert(0,list1.pop(1))
print list1
print list0
Result:
>>>
[u'One', u'Zero', u'Two']
[u'One', u'Zero', u'Two']
>>>

Why does the origin list (list0) also become changed?
Reply With Quote

#2 Old Re: Common python question about lists - 2009-01-22, 16:26

Join Date: Oct 2007
Posts: 2,841
Location: Deva, Romania
bogdan.galiceanu's Avatar
bogdan.galiceanu
Offline
Forum Nokia Champion
Because list1 points to list0, so to speak. If you want to prevent list0 from changing:
Code:
list0 = [u"Zero",u"One",u"Two"]
list1 = list(list0)
list1.insert(0,list1.pop(1))
print list1
print list0
Result:
[u'One', u'Zero', u'Two']
[u'Zero', u'One', u'Two']
Reply With Quote

#3 Old Thumbs up Re: Common python question about lists - 2009-01-22, 16:39

Join Date: Oct 2006
Posts: 30
Location: Austria - Vienna
andipro
Offline
Registered User
That’s very interesting.

I never have seen such effect in other program languages, I think.
When writing an assigning like X = Y afterwards it should not make any difference for X what happens with Y

But thanks for your alternative code example!
Reply With Quote

#4 Old Re: Common python question about lists - 2009-01-22, 20:51

Join Date: Jul 2008
Posts: 41
igordsm
Offline
Registered User
You could also use the slice operator with the default values:

Code:
list0 = [u"Zero",u"One",u"Two"]
list1 = list0[:]
list1.insert(0,list1.pop(1))
print list1
print list0
To learn more about lists in python visit http://effbot.org/zone/python-list.htm


http://igordsm.googlepages.com
Reply With Quote

#5 Old Re: Common python question about lists - 2009-04-17, 19:23

Join Date: Mar 2009
Posts: 2
pedroagb
Offline
Registered User
Hi guys, I think I have a similar problem (python noob here).
I was trying to do a list of lists this way:
Code:
>>> list1=[[0]*2]*3
>>> list1
[[0, 0], [0, 0], [0, 0]]
but when I try to set an element, all elements of the same index change too:
Code:
>>> list1[0][1]=1
>>> list1
[[0, 1], [0, 1], [0, 1]]
But this works as I would expect:
Code:
>>> list2=[[0 for j in range(2)] for i in range(3)]
>>> list2
[[0, 0], [0, 0], [0, 0]]
>>> list2[0][1]=1
>>> list2
[[0, 1], [0, 0], [0, 0]]
Is it because [0]*n creates n references to the same object, not n different objects? I thought it was the short way to initialize an array...
Can someone please explain? Thanks in advance!
Reply With Quote

#6 Old Re: Common python question about lists - 2009-04-17, 21:00

Join Date: Mar 2003
Posts: 125
Location: UK
aya42
Offline
Regular Contributor
Quote:
Originally Posted by pedroagb View Post
Is it because [0]*n creates n references to the same object, not n different objects? I thought it was the short way to initialize an array...
[x]*n will create 'n' copies of 'x' if 'x' is of an immutable type, or 'n' references to 'x' if 'x' is of a mutable type.

Immutable types include: boolean, complex, float, int, long, none, string, tuple, type, unicode.

Mutable types include: class, dictionary, function, instance, list, module.
Reply With Quote

#7 Old Re: Common python question about lists - 2009-04-18, 14:30

Join Date: Mar 2009
Posts: 2
pedroagb
Offline
Registered User
hum... so if I follow you right, when I do [[0]*2]*3 it fails because while in [0]*2 the multiplication operates on an immutable type (int 0 - actually why isnt [0] considered a list too?), in [[0]*2]*3 it does no longer so (cause now it operates on a list [0,0])
That will take a while to soak in, but I can see it makes sense...
Anyway, thanks for your reply, it was helpful
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Trick to prevent Python from closing on red key press. y.a.k Python 19 2009-07-28 15:16
Python for Series 60 released on Forum Nokia! eriksmartt Python 5 2009-07-14 18:00
heap size in python extensions? ecostanza Python 0 2008-04-24 14:59
Ask The Expert: dcrocha on Python Nokia Ron Interviews & Ask the Experts 15 2008-04-21 21:51
Question about Databases supportet by Python GSM1 Python 1 2006-05-30 13:39

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia