Comprehensions

List comprehensions

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another iterable, or to create a subsequence of those elements that satisfy a certain condition.

For example, we can create a list of squares, like

print([x ** 2 for x in [1, 7, 3, 9]])

which is more concise than using a for loop

squares = []

for x in [1, 7, 3, 9]:
    squares.append(x)

print(squares)

The output of both code snippets above is

[1, 49, 9, 81]

Use if to apply a filter, like

print([x ** 2 for x in [1, 7, 3, 9] if x > 4])

which prints

[49, 81]

Dict comprehensions

Similar to list comprehensions, but produces a dictionary instead of a list.

For example, we can create a dictionary of numbers and squares, like

print({x: x ** 2 for x in [1, 7, 3, 9]})

which prints

{1: 1, 7: 49, 3: 9, 9: 81}

Set comprehensions

Warning

Set comprehensions are not yet implemented.

Similar to list comprehensions, but produces a set instead of a list.

For example, we can create a set of squares, like

print({x ** 2 for x in [1, 7, 3, 9]})

which prints

{1, 49, 9, 81}