With two lists of different sizes:
numbers=[1,2,3,4,5]
cities=['LA','NY','SF']
I need to get this:
result={1:'LA', 2:'NY', 3:'SF'}
I thought of doing it with:
result={number:cities[numbers.index(number)] for number in numbers if numbers.index(number)<len(cities)}
But this one-liner gets kind of long. I wonder if there is an alternative way of achieving the same goal.
EDITED LATER:
There were multiple suggestions made to use zip
:
dict(zip(cities, numbers))
While it is a definitely a simpler syntax than list comprehension I've used I wonder which would be faster to execute?