[<expr> for <identifier> in <sequence>]
print([x * x for x in [1, 2, 3]]) # -> [1, 4, 9]{<key expr>: <val expr> for <identifier> in <sequence>}
print({k: v for k, v in zip(['ア', 'カ', 'タ'], ['a', 'ka', 'ta'])}) # -> {'ア': 'a', 'カ': 'ka', 'タ': 'ta'}Syntax is same as for a list, but set comprehensions use {} and generator uses (). The parentheses on a generator comprehension can be removed
when it is the only argument to a function that expects an iterable.
Comprehensions can use multiple generators (for ... in expressions), in which case the rightmost ones produce elements faster.
They can also use conditions:
print([x, y for x in range(-1, 2) for y in range(-1, 2) if x != 0 or y != 0])output: [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)]