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


Groups > comp.lang.python > #77712

Passing a list into a list .append() method

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; "'',": 0.07; 'arguments': 0.09; 'next,': 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; 'worked.': 0.09; 'url:blog': 0.10; 'changes': 0.15; "'b',": 0.16; "'c',": 0.16; "['a',": 0.16; 'desired:': 0.16; 'elements,': 0.16; 'ie.': 0.16; 'message- id:@post.gmane.org': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'tuple': 0.16; 'tuple.': 0.16; 'elements': 0.16; 'passing': 0.19; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'subject: .': 0.24; 'looks': 0.24; "i've": 0.25; 'header:X -Complaints-To:1': 0.27; 'tried': 0.27; 'fixed': 0.29; 'skip:p 30': 0.29; 'subject:list': 0.30; 'along': 0.30; 'url:wiki': 0.31; 'extensively': 0.31; 'reduced': 0.31; 'lists': 0.32; 'another': 0.32; 'running': 0.33; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'add': 0.35; "didn't": 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'error.': 0.37; 'list': 0.37; 'expected': 0.38; 'e.g.': 0.38; 'others.': 0.38; 'skip:[ 10': 0.38; 'to:addr:python- list': 0.38; 'expect': 0.39; 'does': 0.39; 'url:2012': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'applicable': 0.60; 'then,': 0.60; 'subject: ': 0.61; 'today.': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'finally': 0.65; 'due': 0.66; 'here': 0.66; 'url:11': 0.68; 'caused': 0.69; 'manner.': 0.74; 'circles': 0.84; 'received:fios.verizon.net': 0.84; 'received:lsanca.fios.verizon.net': 0.84; 'received:108': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From JBB <jeanbigboute@gmail.com>
Subject Passing a list into a list .append() method
Date Tue, 9 Sep 2014 05:50:29 +0000 (UTC)
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host sea.gmane.org
User-Agent Loom/3.14 (http://gmane.org/)
X-Loom-IP 108.47.76.172 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0)
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.13886.1410242108.18130.python-list@python.org> (permalink)
Lines 121
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1410242108 news.xs4all.nl 2839 [2001:888:2000:d::a6]:57060
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77712

Show key headers only | View raw


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.

1) What does NOT work as desired:

proc_file = []
proc_file = [['a','b','c','d']]
proc_file.append(['A','B','C','D'])
blank_r = ['','','','']

qq = ['aa','bb','cc','dd']
rr = ['inky', 'binky', 'pinky', 'clyde']

for i,j in enumerate(zip(qq,rr)):
    proc_file.append((blank_r))  # Add a row of blanks
    proc_file[i+2][1] = j[0]
    proc_file[i+2][2] = j[1]
    print len(proc_file), blank_r, proc_file
    
print
print
proc_file

3 ['', 'aa', 'inky', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'aa', 'inky', '']]
4 ['', 'bb', 'binky', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'bb', 'binky', ''], ['', 'bb', 'binky', '']]
5 ['', 'cc', 'pinky', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'cc', 'pinky', ''], ['', 'cc', 'pinky', ''], ['', 'cc', 'pinky', '']]
6 ['', 'dd', 'clyde', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['',
'dd', 'clyde', ''], ['', 'dd', 'clyde', ''], ['', 'dd', 'clyde', ''], ['',
'dd', 'clyde', '']]


Out[82]:

[['a', 'b', 'c', 'd'],
 ['A', 'B', 'C', 'D'],
 ['', 'dd', 'clyde', ''],
 ['', 'dd', 'clyde', ''],
 ['', 'dd', 'clyde', ''],
 ['', 'dd', 'clyde', '']]

2) What works as desired:

proc_file = []
proc_file = [['a','b','c','d']]
proc_file.append(['A','B','C','D'])
blank_r = ['','','','']

qq = ['aa','bb','cc','dd']
rr = ['inky', 'binky', 'pinky', 'clyde']

for i,j in enumerate(zip(qq,rr)):
    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
    
print
print
proc_file

3 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', '']]
4 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', ''], ['', 'bb', 'binky', '']]
5 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', ''], ['', 'bb', 'binky', ''], ['', 'cc', 'pinky', '']]
6 ['', '', '', ''] [['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['', 'aa',
'inky', ''], ['', 'bb', 'binky', ''], ['', 'cc', 'pinky', ''], ['', 'dd',
'clyde', '']]


Out[83]:

[['a', 'b', 'c', 'd'],
 ['A', 'B', 'C', 'D'],
 ['', 'aa', 'inky', ''],
 ['', 'bb', 'binky', ''],
 ['', 'cc', 'pinky', ''],
 ['', 'dd', 'clyde', '']]

==== Due diligence

I've read extensively on how arguments are passed to functions but I don't
think they are completely applicable here (though interesting nevertheless)

http://www.jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/


https://www.udacity.com/wiki/common-python-pitfalls

and others.

It looks like .append binds blank_r to proc_file in 1).  I change proc_file
and blank_r changes along with it.  Should I expect this? I understand lists
are mutable but I didn't expect that they could be associated/bound in this
manner.

I first tried "protecting" blank_r by passing it as a tuple.  That caused an
expected mismatch error.

Next, I tried passing it as list(tuple(blank_r)) which worked.  Then, I
finally settled on 2) where I dispensed with the tuple conversion.






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


Thread

Passing a list into  a list .append() method JBB <jeanbigboute@gmail.com> - 2014-09-09 05:50 +0000
  Re: Passing a list into  a list .append() method Paul Kroeger <news@prz-wugen.com> - 2014-09-09 09:12 +0200
    Re: Passing a list into  a list .append() method JBB <jeanbigboute@gmail.com> - 2014-09-09 07:37 +0000
  Re: Passing a list into  a list .append() method Rustom Mody <rustompmody@gmail.com> - 2014-09-09 05:07 -0700

csiph-web