I have several lists of tuples similar to (city_name, city_code) which I store in dictionary for practical reasons so I can easily map city_name to city_code. Obviously both city name and code are unique.
It is easy to retrieve the code of the city based on its name but sometimes I would like to be able to do it another way around like getting the name of the city from the code for which I do linear search among dictionary items which is pretty clumsy.
for c in cities.items():
if c[1] == code:
name = c[0]
break
Is there a a better way to do this? I am using python 2.7.8 and I would like to keep this simple without any additional modules.