logo

... comprehensions: unpack



Notes

Understanding how python does unpacking allows you to create many new variants of comprehensions.

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)]

Feedback? See an issue? Something unclear? Feel free to mention it here.



If you want to be kept up to date, consider signing up for the newsletter.