Python comprehensions are syntactic sugar constructs that provide a way to build a list, dictionary or set from a starting list, dictionary or set whilst altering or filtering elements.
Comprehensions follow mathematical set builder notation rather than map and filter functions.
Make a List that contains the doubled values of a starting list:
You could achieve the same result like this:
…which is considerably more verbose.
List comprehensions have the same effect as the
You can filter a list by adding a conditional statement. A filtered list comprehension takes this form:
comments powered by Disqus