Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28258 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2012-09-02 19:57 +1000 |
| Last post | 2012-09-02 19:57 +1000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Changing a Value in List of lists Chris Angelico <rosuav@gmail.com> - 2012-09-02 19:57 +1000
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-09-02 19:57 +1000 |
| Subject | Re: Changing a Value in List of lists |
| Message-ID | <mailman.75.1346579861.27098.python-list@python.org> |
On Sun, Sep 2, 2012 at 7:44 PM, Rishabh Dixit <rishabhdixit11@gmail.com> wrote: > > Hi all, > > I have a doubt regarding how the list work in following case- > >>>> ls=[[0]*5]*5 >>>> ls[1][1]+=1 >>>> ls > [[0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, > 0, 0, 0]] > > > Here, according to me only one value in ls should be incremented but it is > increasing 1 value in all the lists of ls. Why?? When you take the list [[0,0,0,0,0]] and multiply it by 5, you get a list with five references to the same inner list. That's where the confusion is coming from. Then when you change one of them, it changes all five displayed versions, since they're all still the same list. ChrisA
Back to top | Article view | comp.lang.python
csiph-web