Re: Questions about list-creation

On Monday 30 November 2009 12:22:17 pm Manuel Graune wrote:

when using local variables in list comprehensions, say

a=[i for i in xrange(10)]

the local variable is not destroyed afterwards:
b=list(j for j in xrange(10))

the local variable is destroyed after use:

Actually, [] and list() are not the same. For instance, list(1,2) rises an
error, while [1,2] is the list with two elements.

The comprehension is just a syntactic contruct that allows you to simplify the
creation of lists, while the list() "function" (it is a class, actually)
receives an iterable object and returns a list.

What you seem to be confused about is the construct:

(j for j in xrange(10))

That is called a generator expression, and it is very similar to the list
comprehension, except that it builds an iterator (instead of a list, i.e, the
xrange(10) is not consumed until it is needed), and the loop variable
doesn't "leak" outside.

When you do b=list(j for j in xrange(10)), you are actually doing

b=list((j for j in xrange(10)))

(note the extra set of parenthesis - python lets you ommit those), i.e, you
are calling the list() "function" with a single argument, an iterable that
contains all the elements of xrange(10). You could be calling

foobar(j for j in xrange(10))


And I think I lost my way... I'm sleepy. If I confused you, sorry... and if
I'm helped you, thank you for letting me :D.


Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.

Relevant Pages

  • Clashing of local variable names and symbolic inputs to procedures
    ... convert simple series into continued fraction expansions: ... (I tried it with several lists of rationals, ... changed the names of the argument, and some of the local variables, and then ... possible clash with something symbolic in the input list. ...
  • Re: Questions about list-creation
    ... when using local variables in list comprehensions, ... lists were built using explicit code: ... example you give takes a sequence comprehension as its argument. ...
  • Re: testing for uniquness in a large list
    ... > indicator of what I want to do but I'm hoping for a faster way than the ... > uniqueness - as you can guess this takes an interminable amount of time to ... comprehension for the case in which you need a list lists stays OK). ... function and using local variables IS a major speed win -- local ...
  • Re: change vars in place w/loop or list comprehension
    ... and would be difficult with longer lists. ... That, of course, will work only if they really are globals, and not if they are local variables inside a function. ... If that's the case, you really want to rethink this anyway (or use Raymond's approach, though it still appears to require spelling out your list of names twice). ... If you think you really need to do this anyway, consider posting an example of real code from the program you are trying to write, explaining the actual use case, and we can analyze the real situation instead of debating contrived examples. ...
  • Re: foreach enhancement
    ... that adds lists adds all lists, I don't think its particularly clean to ... With a comprehension exactly what is going to happen is spelled ... > person defines the problem as simple x..y ranges, ... another thinks overlap may be useful ...