If your code sets are large, you can use a dictionary to map one set of codes to another. A dictionary
is easier to update than a complicated conditional statement.
The following example uses a dictionary to map locations to location codes:
locationtable = {
'Toronto': '011',
'New York': '023',
'London': '030',
'Paris': '041',
'Moscow': '055',
'Beijing': '062',
}
if location in locationtable:
locationid = locationtable[location]
else:
# use '000' to represent an unknown location
locationid = '000'
To add a new location, all you need to do is add a new key-value pair to the dictionary: