Tuple Comprehension ???

Michael Torrie torriem at gmail.com
Mon Feb 20 22:56:46 EST 2023


On 2/20/23 20:36, Hen Hanna wrote:
> For a while, i've been curious about a [Tuple Comprehension] 

I've never heard of a "Tuple comprehension." No such thing exists as
far as I know.
> So finally i tried it, and the result was a bit surprising...
>>> X= [ x for x in range(10) ]
> X= ( x for x in range(10) )
> print(X)
> a= list(X)
> print(a)

What was surprising? Don't keep us in suspense!
Using square brackets is a list comprehension. Using parenthesis creates
a generator expression. It is not a tuple. A generator expression can be
perhaps thought of as a lazy list. Instead of computing each member
ahead of time, it returns a generator object which, when iterated over,
produces the members one at a time. This can be a tremendous
optimization in terms of resource usage. See
https://docs.python.org/3/reference/expressions.html#generator-expressions.
 Also you can search google for "generator expression" for other examples.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /