Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28258

Re: Changing a Value in List of lists

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feed.xsnews.nl!border-1.ams.xsnews.nl!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'versions,': 0.05; '[0,': 0.09; 'sep': 0.09; '0],': 0.16; 'confusion': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'incremented': 0.16; 'wrote:': 0.17; 'changes': 0.20; 'all,': 0.21; 'received:209.85.214.174': 0.21; 'displayed': 0.22; 'references': 0.23; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'lists': 0.31; 'subject:lists': 0.32; 'doubt': 0.33; 'subject:List': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'but': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'increasing': 0.75; 'multiply': 0.84; 'subject:Value': 0.91; 'from.': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=En1AY57XOyr564ABO/mZO0krvqqBXS/2zhe1M548Xjg=; b=gulH0/FmhnIAnvIRgGARRkDh3gA0dZ713OTNBHG9nRXb6SAiwEpzF1EXtnm31saZyQ MxlSWN7AtpD53aJ/1rxSRiegwdYO6x0fPYdEP9R79MqhUaVCBGcqAkU8vt/qId5TMEMw qYOL7JAePzqsEGajRT/MKXhFfpzg2JZHTf+e28PgHZWsimY8Lt0eLAjAinc7/Rwfp7jV +ctoF7/dXhGbzIx9HXO1Qg88txPDTdJ649k/cXmQ05Tv6kSV1QzDg+mIPk5cL9GPtdsS lgDwOLQPlBQQHIphzCngxJX82GSN+4c0X88/AdnVFrAwesw7Z5bLPuL8PAU8y6gY2878 xOKg==
MIME-Version 1.0
In-Reply-To <CANFKW=GubXQ8Q7Zr+6dZVp451xi381VhGQHaUZpb1YA9cDs7zA@mail.gmail.com>
References <CANFKW=GubXQ8Q7Zr+6dZVp451xi381VhGQHaUZpb1YA9cDs7zA@mail.gmail.com>
Date Sun, 2 Sep 2012 19:57:37 +1000
Subject Re: Changing a Value in List of lists
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.75.1346579861.27098.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346579861 news.xs4all.nl 6842 [2001:888:2000:d::a6]:56524
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28258

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Changing a Value in List of lists Chris Angelico <rosuav@gmail.com> - 2012-09-02 19:57 +1000

csiph-web