Tuples are iterables. You can use zip
to merge two or more iterables into tuples of two or more elements.
A dictionary can be constructed out of an iterable of 2-tuples, so:
# v values
dict(zip(y,x))
# ^ keys
This generates:
>>> dict(zip(y,x))
{'c': 3, 'a': 1, 'b': 2}
Note that if the two iterables have a different length, then zip
will stop from the moment one of the tuples is exhausted.
You can - as @Wondercricket says - use izip_longest
(or zip_longest
in python-3.x) with a fillvalue
: a value that is used when one of the iterables is exhausted:
from itertools import izip_longest
dict(izip_longest(y,x,fillvalue=''))
So if the key iterable gets exhausted first, all the remaining values will be mapped on the empty string here (so only the last one will be stored). If the value iterable is exhausted first, all remaining keys will here be mapped on the empty string.