9 ms·
If by generic you mean able to use other types of collections, aren't generator expressions[1] the generic version of list comprehensions? List comprehension:
by ptx 7y ago
If by generic you mean able to use other types of collections, aren't generator expressions[1] the generic version of list comprehensions?
List comprehension:
a = [x for x in stuff if x > 1]
Equivalent generic version:
a = list(x for x in stuff if x > 1)
With your own collection type:
a = MyCustomCollection(x for x in stuff if x > 1)
[1] https://www.python.org/dev/peps/pep-0289/ https://www.python.org/dev/peps/pep-0289/