On 1/23/2021 2:54 AM, Unknown wrote:
Le 20/12/2020 à 21:00, danilob a écrit :
b = ((x[0] for x in a))
There is a useless pair of parenthesis
b = (x[0] for x in a)
b is a GENERATOR expression
first list(b) calls next method on b repetedly until b is empty.
So it provides the "content" of b
second list(b) provides nothing since b is empty
(there is no reset on generators)
There is for generator *functions*, which some people also call
generators. For generator expressions, the interpreter makes an
anonymous generator function, calls it, keeping the returned generator.
It deletes the function and 'returns' the generator as the value of
the function.
If one want to iterator twice, write a proper named generator function
with a docstring. In this case
def gf(a):
"Return a generator that yields the first item of each sequence in a."
for x in a:
yield x[0]
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list