0

I would like to assign countries a currency value, but some currencies are used in multiple countries. I know I can just assign them individually for every country but is there a better way of doing it? Something I tried was:

rates = { ('England', 'wales'): 'GBP' }

where the expected result was:

rates['England'] = GBP
rates['wales'] = GBP

This doesn't work though. What can I do?

3 Answers3

4

There isn't any syntactic sugar for that use case, but you could use a few methods that aren't too verbose to get the same result:

fromkeys

# first argument are all keys, second is default value (or None)
rates = dict.fromkeys(('England', 'wales'), 'GBP')

comprehension

rates = {k: 'GBP' for k in ('England', 'wales')}
Cireo
  • 4,197
  • 1
  • 19
  • 24
0

You could perhaps just flip the two?

rates = { 
"GBP": ["England", "Wales"], 
"EUR": ["Germany", "Fance", "Netherlands"] 
}
0

You can create lists of countries and add them all in one loop:

GBP_countries=['England', 'wales']
rates = {}
for country in  GBP_countries:
    rates.update({country:'GBP'})
flabons
  • 331
  • 1
  • 8