Python Forum
[split] merge/display values from two dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] merge/display values from two dictionaries
#1
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'
Reply
#2
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.
Reply
#3
(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)
Reply
#4
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 45,876 May-06-2023, 08:14 AM
Last Post: pramod08728
  [split] formula for validating monetary values? kakos_k9 1 749 Dec-17-2022, 09:28 PM
Last Post: woooee
  How to split file by same values from column from imported CSV file? Paqqno 5 2,764 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  How to display combobox related values in labels Lalremruata 0 1,556 Jul-20-2021, 02:45 AM
Last Post: Lalremruata
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,211 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  5 variants to invert dictionaries with non-unique values Drakax1 2 2,595 Aug-31-2020, 11:40 AM
Last Post: snippsat
  Accessing values in list of dictionaries pythonnewbie138 2 2,120 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138
  Get values from a split column Pythonito 3 2,093 Apr-18-2020, 11:35 PM
Last Post: Larz60+
  [split] capitalize dict keys for display in string newbieAuggie2019 3 2,980 Oct-10-2019, 10:50 AM
Last Post: perfringo
  Passing Values of Dictionaries to Function & Unable to Access Them firebird 3 2,571 Aug-03-2019, 10:25 PM
Last Post: firebird

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020