> 9. Create dict from keys and values
using zip
In 2.7+, I'd recommend a dictionary comprehension instead.
In 2.7+, I'd recommend a dictionary comprehension instead.
{k: v for k, v in zip(keys, values)}
edit:As I mention below, this becomes useful when you want to do e.g.
{f(k): g(v) for k, v in zip(keys, values)} dict(zip(keys, value))
is more concise, doesn't introduce extra variables, and doesn't repeat itself, and explicitly names a dict rather than using a symbol. {k.upper(): v ** 2 for k, v in zip(keys, values)}
Or generally {f(k): g(v) for k, v in zip(keys, values)}
I find this very extensible. In terms of conciseness, they both fit on a single line and I like the whitespace inside a dict comprehension.