Calmcode - comprehensions: unpack

Unpack

1 2 3 4 5 6 7 8 9

Understanding how python does unpacking allows you to create many new variants of comprehensions. The code below gives a good demonstration of this.

arr = [('a', 1), ('b', 2), ('c', 2)]
for idx, (char, i) in enumerate(arr):
    print(idx, char, i)

Understanding this unpacking allows for very expressive one-liners.

[{key: value, 'i': idx} for idx, (key, value) in enumerate(arr)]