DEV Community

Andrés Baamonde Lozano
Andrés Baamonde Lozano

Posted on

List/Dict comprehension

List/Dict comprehension is a simple way to create lists and dicts with a bunch of values that we need. For really take advantage of this operator we must combine it with lambda operator, but first ,we will see simple examples.


[x * 2 for x in range(3)]
# output: [0, 2, 4]

{chr(65+x) : x for x in range(3)}
# output: {'A': 0, 'C': 2, 'B': 1}
Enter fullscreen mode Exit fullscreen mode

Now we will combine both operators:

serie = [x for x in range(20)]
f = lambda x: chr(x + 66) if x % 2 == 0 else chr(x + 65)
[f(x) for x in serie]
# output: ['B', 'B', 'D', 'D', 'F', 'F', 'H', 'H', 'J', 'J', 'L', 'L', 'N', 'N', 'P', 'P', 'R', 'R', 'T', 'T']
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
joshuatbadger profile image
joshuatbadger

Alternatively, you can still one-line this.

[chr(x + 66) if x % 2 == 0 else chr(x + 65) for x in range(20)]