This is easy. You have a dict
mapping each country to a list of timezones. You want to map each list
member back to the dict
.
Rather than just give the answer, let's see how to get it.
First, if you just had a dict
mapping each country to a single timezone, this would be a simple reverse mapping:
timezone_countries = {timezone: country
for country, timezone in country_timezones.iteritems()}
But this won't work; you have a mapping to a list of timezones, and you want each timezone in that list to map back to the country. That English description "each timezone in that list" is trivially translatable to Python:
timezone_countries = {timezone: country
for country, timezones in country_timezones.iteritems()
for timezone in timezones}
Here it is in action:
>>> from pytz import country_timezones
>>> timezone_countries = {timezone: country
for country, timezones in country_timezones.iteritems()
for timezone in timezones}
>>> timezone_countries['Europe/Zurich']
u'CH'
Side note: You didn't mention Python 2 vs. 3, so I assumed 2. If you're on 3, change iteritems
to items
, and the output will be 'CH'
instead of u'CH'
.