Passing a list into a list .append() method
jeanbigboute at gmail.com
Tue Sep 9 09:08:57 CEST 2014
Frank Millman <frank <at> chagford.com> writes:
> "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.
> I am sure that someone will give you a comprehensive answer, but here is a
> quick clue which may be all you need.
[ Deletia per gmane's requirements ]
> 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 [*]
> Frank Millman
Ok, this does clear up why the list() construction worked in this context -
I wasn't aware that it would create a copy.
I'm still a little confused by why passing the list as an argument causes
the list to change. But, I was not aware of the id() method to see what's
equivalent to what. I'll experiment with this and you've given me some good
ideas on other docs I need to read.
Thank you for the quick reply.
More information about the Python-list