Home > Back-end >  How To Add Specific Keys In Nested Dictionary In Python
How To Add Specific Keys In Nested Dictionary In Python

Time:11-11

I may be formatting this dictionary wrong (my first time doing this)

I have a dictionary of every province with corrected ID and added it to value "Canada". I'm trying to add the population of ALL the provinces in the nested dictionary

    ontario = dict(capital="Toronto", largest="Toronto", population=14826276)
    quebec = dict(capital="Quebec City", largest="Montreal", population=8604495)
    nova_Scotia = dict(capital="Halifax", largest='Halifax', population=992055)
    new_Brunswick = dict(capital="Fredricton", largest='Moncton', population=789225)
    manitoba = dict(capital="Winnipeg", largest="Winnipeg", population=1383765)


canada = {ontario, quebec, nova_Scotia, new_brunswick, manitoba, british_columbia, prince_edward_island, saskatchewan, alberta, newfoundland_and_labrador}


 for key, value in canada.items():
        if value and 'population' in value.keys():
            # Adding all values of population to receive total population of canada
            sum  = value['population']
            print(sum)

thanks again in advance.

CodePudding user response:

  1. I only added the listed 5 provinces into the nested dictionary.
  2. I used a for loop to calculate the total population of Canada (the sum of the 5 listed provinces).

Note that my nested dictionary has the same format as a normal dictionary, "a key : value" --> "1 : ontario"

ontario = dict(capital="Toronto", largest="Toronto", population=14826276)
quebec = dict(capital="Quebec City", largest="Montreal", population=8604495)
nova_Scotia = dict(capital="Halifax", largest='Halifax', population=992055)
new_Brunswick = dict(capital="Fredricton", largest='Moncton', population=789225)
manitoba = dict(capital="Winnipeg", largest="Winnipeg", population=1383765)

canada = {1:ontario, 2:quebec, 3:nova_Scotia, 4:new_Brunswick, 5:manitoba}
#canada = {ontario, quebec, nova_Scotia, new_brunswick, manitoba, british_columbia, prince_edward_island, saskatchewan, alberta, newfoundland_and_labrador}

sum = 0
for providence in canada:
    # Adding all values of population to receive total population of canada
    sum  = (canada[providence]["population"])

print(sum)

CodePudding user response:

You didn't create dictionary but set (which doesn't have keys)

To create dictionary you would need keys like

canada = {1:ontario, 2:quebec, 3:nova_scotia, 4:new_brunswick, 5:manitoba}

canada = {"Ontario":ontario, "Quebec":quebec, "Nova Scotia":nova_scotia, "New Brunswick":new_brunswick, "Manitoba":manitoba}

and then you can use canada.items() and sum population

(I use variable total because there is function sum())

# --- before `for`-loop ---

total = 0

# --- `for`-loop ---

for key, value in canada.items():
    total  = value['population']

# --- after `for`-loop ---

print(total)

or shorter

total = sum(value['population'] for value in canada.values())

and then you can add to this dictionary

canada['total'] = total

Full code:

ontario = dict(capital="Toronto", largest="Toronto", population=14826276)
quebec = dict(capital="Quebec City", largest="Montreal", population=8604495)
nova_scotia = dict(capital="Halifax", largest='Halifax', population=992055)
new_brunswick = dict(capital="Fredricton", largest='Moncton', population=789225)
manitoba = dict(capital="Winnipeg", largest="Winnipeg", population=1383765)

canada = {1:ontario, 2:quebec, 3:nova_scotia, 4:new_brunswick, 5:manitoba}#, british_columbia, prince_edward_island, saskatchewan, alberta, newfoundland_and_labrador

total = 0
for key, value in canada.items():
    total  = value['population']
print(total)

#total = sum(value['population'] for value in canada.values())

canada['total'] = total

print(canada)
  • Related