Combining lists to dictionary
Ben Finney
ben+python at benfinney.id.au
Tue Nov 11 20:16:59 EST 2014
Denis McMahon <denismfmcmahon at gmail.com> writes:
> Hi
>
> Given x,y are a lists of keys and value that I wish to combine to a
> dictionary, such that x[n] is the key for value y[n], which is preferred:
>
> z = {a:b for (a,b) in zip(x,y)}
This one, with the caveat to use PEP-8 compatible formatting::
z = {a: b for (a, b) in zip(x, y)}
> z = {x[n]:y[n] for n in range(min(len(x),len(y)))}
Too much indirection for no gain that I can see.
> The zip feels more elegant, but it seems clunky to use the zip method to
> create a list of tuples just to split them up into key:value pairs.
I think ‘zip’ has this as a primary intended purpose, so it seems fine
to me.
--
\ “Of all classes the rich are the most noticed and the least |
`\ studied.” —John Kenneth Galbraith, _The Age of Uncertainty_, |
_o__) 1977 |
Ben Finney
More information about the Python-list
mailing list