An example would be better to understand this.
Calling next
method of generator
to yield each element.
>>> a = (i for i in range(4))
>>> a.next()
0
>>> a.next()
1
>>> a.next()
2
>>> a.next()
3
>>> a.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>> list(a)
[]
Now calling list
function on our generator object.
>>> a = (i for i in range(4))
>>> list(a)
[0, 1, 2, 3]
>>> list(a)
[]
Now calling list
on our list comprehension.
>>> a = [i for i in range(4)]
>>> list(a)
[0, 1, 2, 3]
>>> list(a)
[0, 1, 2, 3]
So list comprehension and dict comprehension are similar which results in actual data not like generator which yields element.