Dict comprehension (shorthand for loops)
Tags: @dictionary @comprehension @for @loop
Dictionary comprehension is basically a shorthand for loop. Looks very ugly and not readable at all so avoid using this if possible:
x = ['something', 'else', 'chump']
d = {}
# This..
for k in x:
d[k] = 999
# ...does the same thing as this
d = {k: 999 for k in x}
# OUTPUT
>>> d
>>> {'something': 999, 'else': 999, 'chump': 999}
# You can also do:
e = {k: v for k, v in d.items()}
>>> e
>>> {'something': 999, 'else': 999, 'chump': 999}