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


Groups > comp.lang.python > #77713 > unrolled thread

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

Started by"Frank Millman" <frank@chagford.com>
First post2014-09-09 08:53 +0200
Last post2014-09-09 08:53 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Passing a list into  a list .append() method "Frank Millman" <frank@chagford.com> - 2014-09-09 08:53 +0200

#77713 — Re: Passing a list into a list .append() method

From"Frank Millman" <frank@chagford.com>
Date2014-09-09 08:53 +0200
SubjectRe: Passing a list into a list .append() method
Message-ID<mailman.13887.1410245614.18130.python-list@python.org>
"JBB" <jeanbigboute@gmail.com> wrote in message 
news:loom.20140909T073428-713@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


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web