Questions about list-creation

Stephen Hansen apt.shansen at gmail.com
Mon Nov 30 14:52:08 EST 2009


On Mon, Nov 30, 2009 at 9:22 AM, Manuel Graune <manuel.graune at koeln.de>wrote:

> in (most) python documentation the syntax "list()"
> and "[]" is treated as being more or less the same
> thing. For example "help([])" and "help(list())" point
> to the same documentation. Since there are at least
> two cases where this similarity is not the case, (see below)
> can someone explain the reasoning behind this and point to
> further / relevant documentation?
> (To clarify: I am not complaining about this, just asking.)
>

They -do- pretty much the same thing, but you can't quite treat them as two
forms which can be exchanged at a whim.

[] is the syntax for creating a list literal in source code. Its also the
syntax for list comprehensions. A list comprehension is an expression that
constructs a list all at once.

list() is a function (type/constructor/...) which accepts any iterable as an
argument (or None), and uses it to construct a list, returning it.

Those two things are very different, although in the end they both produce
lists: one is syntax, the other is a function which has to be called.

1.)
>
> when using local variables in list comprehensions, say
>
> a=[i for i in xrange(10)]
>
> the local variable is not destroyed afterwards:
>
> print "a",a
> print "i",i
>
> using the similar code
>

List comprehensions "leaked", as more of an implementation detail I believe
then anything else: in Python 3 it was removed, to better match generator
expressions (IIRC).


>
> b=list(j for j in xrange(10))
>
> the local variable is destroyed after use:
>
> print "b",b
> print "j",j
>
>
But see, this is something completely different.

    (j for j in xrange(10))

is NOT a list comprehension. Its a generator comprehension. In the first
example with lists, you're using literal syntax to construct (all at once) a
list. In this, you're using literal syntax to construct a generator-- the
generator is then passed to the list function, which iterates over it and
constructs a list, returning when that generator is exhausted.

And generator comprehensions don't leak their variables (I can't remember if
that was an implementation detail or a deliberate design decision, but
eventually list comprehensions were made to match the behavior).



> and 2)
>
> a=list([])
>
> vs.
>
> b=[[]]
>

Those are also doing two completely different things: on the first, you are
using literal syntax to create an empty list. You are then passing this list
(which is an iterable) to the list function, telling it to make a list out
of that literal. It does so, which is in the end a (shallow) copy of the
first list you made.

The second, you are using literal syntax to create an empty list, and using
literal syntax to place that empty list inside another list.

You basically can't treat the two things as interchangable, because they're
different: one is syntax, one is a function. Both are ways to make lists.

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091130/73004c14/attachment.html>


More information about the Python-list mailing list