Python Forum
Nested dictionaries question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested dictionaries question
#1
Hi everyone,
I am a bit confused on this concept.

I understand it is a dictionary of dictionaries but here is where I am confused: A dictionary contains key / value pairs.

Well here we have a nested dictionary:

kpis_dict = {'transfer_dummy': {'unit': '%', 'label': 'transfer', 'classifier': 1, 'id': 11, 'direction': '+', 'demo': 1, 'value': 900}, 'callback_dummy': {'unit': '%', 'label': 'call back', 'classifier': 1, 'id': 10, 'direction': '+', 'demo': 0, 'value': 900}, 'appt_dummy': {'unit': '%', 'label': 'appointment', 'classifier': 1, 'id': 12, 'direction': '+', 'demo': 1, 'value': 900}}
kpis_dict2 = {id: kpi_dict for id, kpi_dict in kpis_dict.items()}

test = kpis_dict.values()
a= list(test)
print (a)
I am trying to swap out id with transfer dummy. So basically take the key and swap it out with what I thought was another key (id being the key to value 11)

When I use the keys method I get this:
['transfer_dummy', 'callback_dummy', 'appt_dummy']

When I use the values method I get this:
[{'unit': '%', 'label': 'transfer', 'classifier': 1, 'id': 11, 'direction': '+', 'demo': 1, 'value': 900}, {'unit': '%', 'label': 'call back', 'classifier': 1, 'id': 10, 'direction': '+', 'demo': 0, 'value': 900}, {'unit': '%', 'label': 'appointment', 'classifier': 1, 'id': 12, 'direction': '+', 'demo': 1, 'value': 900}]

So it appears keys in dictionaries only nest 1 level deep?

I understand how to swap out the keys from method but i can't figure out how to swap out the id key and why python is calling this a value.
Reply
#2
It's not about the level. The key and values methods of a dictionary are for the object itself. So you get the keys of kriss_dact - three of them. When you want the values these values are dictionaries. The values method of the kriss_dict is not recursive. It gets the values of the object and it does not matter what these objects/values are.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Then is it possible to run keys or values on the sub dictionaries? I don't understand how I can access only specific values in nested dictionaries. The keys is very easy the values (more over specific ones) not so much.
Reply
#4
I think this will help you understand the relationships:
kpis_dict = {'transfer_dummy': {'unit': '%', 'label': 'transfer', 'classifier': 1, 'id': 11,
                                'direction': '+', 'demo': 1, 'value': 900},
             'callback_dummy': {'unit': '%', 'label': 'call back', 'classifier': 1, 'id': 10,
                                'direction': '+', 'demo': 0, 'value': 900},
             'appt_dummy': {'unit': '%', 'label': 'appointment', 'classifier': 1, 'id': 12,
                            'direction': '+', 'demo': 1, 'value': 900}}

def print_dict():
    for key, value in kpis_dict.items():
        print('{}'.format(key))
        for sub_key, sub_value in value.items():
            print('    {}: {}'.format(sub_key, sub_value))

print_dict()
which displays:
Output:
transfer_dummy unit: % label: transfer classifier: 1 id: 11 direction: + demo: 1 value: 900 callback_dummy unit: % label: call back classifier: 1 id: 10 direction: + demo: 0 value: 900 appt_dummy unit: % label: appointment classifier: 1 id: 12 direction: + demo: 1 value: 900
You can access any element using syntax like:
print('callback_dummy Id = : {}'.format(kpis_dict['callback_dummy']['id']))
Output:
callback_dummy Id = : 10
Reply
#5
If you want the nested dicts values you can loop over the values ( which are dictionaries ) and get their values.

for value in kpis_dict.values(): # the value is a dictionary
    print(value.values())
Getting a specific value:

kpis_dict['transfer_dummy']['id']
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
This is a lot of information to take in. If the "subkeys" are actually values in Python how can you even edit them? I understand how to change key and value (in this case value of what I am calling a "subkey") but how do you change the actual "subkey"? For example I want to swap out the id "subkey" (value) with a value and I can't find the syntax to change it.

Not even sure if my question makes sense.
Reply
#7
you would use for exanple:
kpis_dict['callback_dummy']['id'] = 25
Reply
#8
(Jan-31-2018, 12:04 AM)Larz60+ Wrote: you would use for exanple:
kpis_dict['callback_dummy']['id'] = 25

This changes id from 10 to 25. I want to actually change the word / key / value "id" itself. From id to say newid or whatever.

I knew my question was poorly written!

I re-wrote twice too lol
Reply
#9
Ok, so say you want to replace 'id' with 'id_new'
use:
kpis_dict['callback_dummy']['id_new'] = kpis_dict['callback_dummy'].pop('id')
Reply
#10
That last point is super helpful. I figured out another way to solve it before I read it.

I am pretty sure this is a terrible way and there has to be something better but this works.

import ast
kpi_dicts = {'transfer_dummy': {'unit': '%', 'label': 'transfer', 'classifier': 1, 'id': 11, 'direction': '+', 'demo': 1, 'value': 900}, 'callback_dummy': {'unit': '%', 'label': 'call back', 'classifier': 1, 'id': 10, 'direction': '+', 'demo': 0, 'value': 900}, 'appt_dummy': {'unit': '%', 'label': 'appointment', 'classifier': 1, 'id': 12, 'direction': '+', 'demo': 1, 'value': 900}}
kpi_dict2 = {kpi: kpi_dicts for kpi, kpi_dicts in kpi_dicts.items()}

test = kpi_dicts.keys()
a = test
listnumbers = (11, 10, 12)
a = list(test)
b = "kpi"
for x in a:
    kpi_dicts[x][b]=x
for x, y in zip(test, listnumbers):
    kpi_dicts[y] = kpi_dicts.pop(x)
new_dict1 = ast.literal_eval(str(kpi_dicts).replace("'id':","'kpi':"))


print (new_dict1)
Reply


Forum Jump:

User Panel Messages

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