Generator expression resemble list comprehensions. List comprehensions are specified within [] brackets and they produce the complete list of items at once. On the other hand, generator expressions, which are specified using () brackets, return the same items but one at a time.
EXAMPLE:
a=range(6)
print("List Comprehension", end=':')
b=[x+2 for x in a]
print(b)
print("Generator expression", end=':n')
c=(x+2 for x in a)
print(c)
for y in c:
print(y)
OUTPUT:
List Comprehension:[2, 3, 4, 5, 6, 7]
Generator expression:
<generator object <genexpr> at 0x0000016362944480>
2
3
4
5
6