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


Groups > comp.lang.python > #77716

Re: Passing a list into a list .append() method

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subsequent': 0.05; 'assignment': 0.07; '[1,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'subject:into': 0.09; 'subject:method': 0.09; 'template': 0.14; 'changes': 0.15; '3],': 0.16; 'desired:': 0.16; 'elements,': 0.16; 'ie.': 0.16; 'iteration': 0.16; 'loop.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'elements': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'subject: .': 0.24; 'header:X-Complaints-To:1': 0.27; 'fixed': 0.29; 'skip:p 30': 0.29; 'list:': 0.30; 'subject:list': 0.30; '"",': 0.31; 'reduced': 0.31; 'reflected': 0.31; 'another': 0.32; 'running': 0.33; 'actual': 0.34; "i'd": 0.34; 'problem': 0.35; 'but': 0.35; 'add': 0.35; 'false': 0.36; 'two': 0.37; 'list': 0.37; 'e.g.': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'subject: ': 0.61; 'today.': 0.61; 'new': 0.61; 'more': 0.64; 'effectively': 0.66; 'circles': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Passing a list into a list .append() method
Date Tue, 09 Sep 2014 09:13:16 +0200
Organization None
References <loom.20140909T073428-713@post.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd8a52.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13889.1410246814.18130.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1410246814 news.xs4all.nl 2881 [2001:888:2000:d::a6]:45103
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77716

Show key headers only | View raw


JBB wrote:

> I have a list with a fixed number of elements which I need to grow; ie.
> add rows of a fixed number of elements, some of which will be blank.
> 
> e.g. [['a','b','c','d'], ['A','B','C','D'], ['', 'aa', 'inky', ''], ['',
> 'bb', 'binky', ''], ... ]
> 
> This is a reduced representation of a larger list-of-lists problem that
> had me running in circles today.
> 
> I think I figured out _how_ to get what I want but I am looking to
> understand why one approach works and another doesn't.

The actual problem is that you are appending the same list multiple times. 
In nuce:

>>> inner = [1, 2, 3]
>>> outer = [inner, inner, inner]

Remember that outer[0] is inner just like the two other items of the outer 
list:

>>> outer[0] is inner
True
>>> outer[1] is inner
True
>>> outer[2] is inner
True

So

>>> outer[0][0] = 42

effectively changes inner

>>> inner
[42, 2, 3]

which is reflected in the following output:

>>> outer
[[42, 2, 3], [42, 2, 3], [42, 2, 3]]

With list(inner) you create a (shallow) copy of inner

>>> inner = [1, 2, 3]
>>> outer = [list(inner), list(inner), list(inner)]
>>> outer[0] is inner
False

and a subsequent assignment changes only that specific copy of inner:

>>> outer[0][0] = 42
>>> outer
[[42, 2, 3], [1, 2, 3], [1, 2, 3]]

So the key to the solution is that you create a new list on every iteration 
of the loop. With that in mind I'd write

for a, b, in zip(qq, rr):
    proc_file.append(["", a, b, ""])

or alternatively if the actual inner list is more complex

template = ["", "", "", ""]
for p in zip(qq, rr):
    inner = list(template)
    inner[1:3] = p
    proc_file.append(inner)

> 1) What does NOT work as desired:
>     proc_file.append((blank_r))  # Add a row of blanks
>     proc_file[i+2][1] = j[0]
>     proc_file[i+2][2] = j[1]

> 2) What works as desired:

>     proc_file.append(list(blank_r))  # Change it to list(blank_r) and it
>     works 
>     proc_file[i+2][1] = j[0]
>     proc_file[i+2][2] = j[1]
>     print len(proc_file), blank_r, proc_file

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Passing a list into  a list .append() method Peter Otten <__peter__@web.de> - 2014-09-09 09:13 +0200

csiph-web