In article <[email protected]>, Ned Deily <[email protected]> wrote:
> In article > <[email protected]>, > Benjamin Kaplan <[email protected]> wrote: > > On Sat, Dec 12, 2009 at 7:15 PM, Tom Machinski <[email protected]> > > wrote: > > > In most cases, `list(generator)` works as expected. Thus, > > > `list(<generator expression>)` is generally equivalent to `[<generator > > > expression>]`. > > Actually, it's list(generator) vs. a list comprehension. I agree that > > it can be confusing, but Python considers them to be two different > > constructs. > > > > >>> list(xrange(10)) > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > > >>> [xrange(10)] > > [xrange(10)] > > That's not a list comprehension, that's a list with one element. > > >>> [x for x in xrange(10)] > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > > <CrocodileDundee> Now *that's* a list comprehension. </CrocodileDundee> Which is not quite the point Benjamin was trying to make - sorry! Consulting the adjacent sections on "List displays" and "Generator expressions" in the Language Reference: http://docs.python.org/reference/expressions.html#list-displays for generator expressions "the parentheses can be omitted on calls with only one argument " but the expressions in a list_comprehension are not in a call context. So there is no ambiguity: [<generator expression>] requires parens around the generator expression and that list display produces a list with one element as Benjamin points out. -- Ned Deily, [email protected] -- http://mail.python.org/mailman/listinfo/python-list
