Passing a list into a list .append() method

JBB jeanbigboute at gmail.com
Tue Sep 9 01:50:29 EDT 2014


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.










More information about the Python-list mailing list