Passing a list into a list .append() method

Frank Millman frank at chagford.com
Tue Sep 9 08:53:20 CEST 2014


"JBB" <jeanbigboute at gmail.com> wrote in message 
news:loom.20140909T073428-713 at post.gmane.org...
>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.
>
[...]
>
> 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.
>

I am sure that someone will give you a comprehensive answer, but here is a 
quick clue which may be all you need.

>>> x = [1, 2, 3]
>>> id(x)
16973256
>>> y = x
>>> id(y)
16973256
>>> z = list(x)
>>> id(z)
17004864

Wrapping a list with 'list()' has the effect of making a copy of it.

This is from the docs (3.4.1) -

"""
Lists may be constructed in several ways:

- Using a pair of square brackets to denote the empty list: []
- Using square brackets, separating items with commas: [a], [a, b, c]
- Using a list comprehension: [x for x in iterable]
- Using the type constructor: list() or list(iterable)

The constructor builds a list whose items are the same and in the same order 
as iterable's items.
iterable may be either a sequence, a container that supports iteration, or 
an iterator object.
If iterable is already a list, a copy is made and returned, similar to 
iterable[:]. [*]
For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) 
returns [1, 2, 3].
If no argument is given, the constructor creates a new empty list, [].
"""

I marked the relevant line with [*]

HTH

Frank Millman






More information about the Python-list mailing list