Python Forum

Full Version: [split] merge/display values from two dictionaries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following problem:
I created two dictionaries

province = {'onatario':'on', 'alberta':'ab', 'quebec':'qc', 'british_columbia':'bc'}
capitals = {'on':'toronto', 'ab':'edmonton', 'qc':'montreal', 'bc':'victoria'}
result = dict()
for key, value in province.items():
    result[key] = capitals[value]
1. this doesn't produce any results
2. I want output as 'the capital of Ontario is toronto'
what your code is doing is to populate result dict with key:value pairs, combining province and capitals.
If you want some output, you should add some code to print what you want.
(Oct-10-2017, 04:55 AM)ramkumar_70 Wrote: [ -> ]1. this doesn't produce any results
You don't print anything out, so there's no output.  Try adding this to the end: print(result)
The question why you don't get any result is already answered. Mostly it's all about data structures. Do you think that you have chosen the right data structure?

Maybe you can combine the data into one dict:

canada_provinces = {
    "canonical province name": {
        "shortcut": "shortcut of province",
        "capital": "capital of province",
        "population": 0,
        ...: ...
        },
    }
By the way, the example is valid code. Don't ask why :-D