What's new

The most simple way to make a dictionary with key list

Fever

Active member
Staff member
I would like to make a dictionary as follows.

{'US': 'NA',
'CA': 'NA',
'GE': 'EU',
'UK': 'EU',
'FR': 'EU',
'CN': 'ASIA',
'JP': 'ASIA',
'KR': 'ASIA',
'BR': 'SA',
'AG': 'SA'}


I know how to make it. However in reality there are a lot of keys, so I'd like to know more simple way to make this dictionary

Thanks.


CTY1 = ['US', 'CA']
CTY2 = ['GE','UK','FR']
CTY3 = ['CN','JP','KR']
CTY4 = ['BR','AG']

DICT = {x : 'NA', y : 'EU', w : 'ASIA', z : 'SA' for x, y, w, z in zip(CTY1, CTY2, CTY3, CTY4)}
# I tried as above but it doesn't work.
# So I write as below. I'd like to find more simple way.

DICT1 = {x : 'NA' for x in CTY1}
DICT2 = {x : 'EU' for x in CTY2}
DICT3 = {x : 'ASIA' for x in CTY3}
DICT4 = {x : 'SA' for x in CTY4}

DICT = {}
DICT.update(DICT1)
DICT.update(DICT2)
DICT.update(DICT3)
DICT.update(DICT4)

Continue reading...
 
Top