So what's the explanation behind the difference between list()
and dict()
in the following example:
glist = (x for x in (1, 2, 3))
print(list(glist))
print(list(glist))
gdict = {x:y for x,y in ((1,11), (2,22), (3,33))}
print(dict(gdict))
print(dict(gdict))
>>>
[1, 2, 3]
[]
{1: 11, 2: 22, 3: 33}
{1: 11, 2: 22, 3: 33}
The difference is that only the first expression
glist
is a generator, the second onegdict
is adict
-comprehension. The two would only be equivalent, if you'd change the first one for[x for x in (1, 2, 3)]
.A comprehension is evaluated immediately.