Passing a list into a list .append() method
Rustom Mody
rustompmody at gmail.com
Tue Sep 9 08:07:54 EDT 2014
On Tuesday, September 9, 2014 11:25:29 AM UTC+5:30, 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.
> 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', '']]
Dont quite know what you are trying to do.
Does this serve your purpose?
>>> proc_file = [['a','b','c','d']]
>>> proc_file.append(['A','B','C','D'])
>>> blank_r = ['','','','']
>>> [[i,j0,j1, ""] for (i, (j0, j1)) in enumerate(zip(qq,rr))]
[[0, 'aa', 'inky', ''], [1, 'bb', 'binky', ''], [2, 'cc', 'pinky', ''], [3, 'dd', 'clyde', '']]
Or if you prefer:
>>> proc_file + [[i,j0,j1, ""] for (i, (j0, j1)) in enumerate(zip(qq,rr))]
[['a', 'b', 'c', 'd'], [0, 'aa', 'inky', ''], [1, 'bb', 'binky', ''], [2, 'cc', 'pinky', ''], [3, 'dd', 'clyde', '']]
More information about the Python-list
mailing list