3 ms·
Comprehending Python's (List|Dict|Set) Comprehensions
- ericfrederich 10y ago... don't forget generator comprehension l = [x**2 for x in range(20) if x**2 % 2 == 0] s = {x**2 for x in range(20) if x**2 % 2 == 0} d = {x: x**2 for x in range(20) if x**2 % 2 == 0} g = (x**2 for x in range(20) if x**2 % 2 == 0) you can use that to create an infinite generator g = (x**2 for x in itertools.count() if x **2 % 2 == 0) for thing in g: print(thing)