For comprehensions¶
A list comprehension maps each element of a list:
Add a when clause to filter elements:
A map comprehension uses key => value:
For maps, bind both key and value:
Use ... after the value to group repeated keys into lists:
Comprehensions can nest:
[
for r in input.regions :
{ region: r, names: [ for s in input.services : s.name when s.region == r ] }
]
Filters also narrow optional values. In this example, u.port is known to be an integer in the value expression:
A splat is a shortened form of comprehension. The following are equivalent: