Python Forum
Lists in Dictionaries Assistance - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Lists in Dictionaries Assistance (/thread-9935.html)



Lists in Dictionaries Assistance - wfsteadman - May-05-2018

greetings all,
I have a file given to me that is in YAML format

Quote:---
device_list:
location1:
department: [FIN]
description: location1
devices: [router1, router2]
location2:
department: [FIN]
description: location2
devices: [router3, router4, router5]
pwgroup: admin
location3:
department: [ACC]
description: location3
devices: [router6]
location4:
department: [ACC]
description: location4
devices: [router7, router8, router9]
location5:
pwgroup: admin
department: [ADM]
description: location5
devices: [router10, router11, router12]
location6:
department: [FIN]
pwgroup: admin
description: location6
devices: [router13, router14, router15, router16]
location2:
department: [FIN]
description: location7
devices: [router17]
pwgroup: admin

I have built out the part that gets the list into python dictionary:

import yaml
    
    with open('router.cfg', 'r') as lb:
        try:
            test = (yaml.load(lb))
        except yaml.YAMLError as exc:
            print(exc)
    
    maindict = test['device_list']
    
    for key, value in maindict.items():
        print(key, value)
the above returns the following:

Quote:location1 {'department': ['FIN'], 'description': 'location1', 'devices': ['router1', 'router2']}
location2 {'department': ['FIN'], 'description': 'location7', 'devices': ['router17'], 'pwgroup': 'admin'}
location3 {'department': ['ACC'], 'description': 'location3', 'devices': ['router6']}
location4 {'department': ['ACC'], 'description': 'location4', 'devices': ['router7', 'router8', 'router9']}
location5 {'pwgroup': 'admin', 'department': ['ADM'], 'description': 'location5', 'devices': ['router10', 'router11', 'router12']}
location6 {'department': ['FIN'], 'pwgroup': 'admin', 'description': 'location6', 'devices': ['router13', 'router14','router15', 'router16']}

here is where I get totally lost and would appreciate some pointers or assistance

I want to loop through each of the entries and if the department is FIN then I want it to put the devices in that entry into a list that I will be able to process through. The file is a list of routers in different locations. So the output I would be looking to be in the new list would be:

router1 router2 router17 router13 router14 router15 router16

Not very well versed in dictionaries and lists in dictionaries and all the searches I have found have confused me for some reason.

Thanks in Advance
Wally


RE: Lists in Dictionaries Assistance - buran - May-05-2018

here is more general example that will create a dict with devices for each department
note that in your data 'department' is a list, even with one value. I don't know if it is possible to have more than one department in this list

import yaml
from collections import defaultdict
     
with open('router.cfg', 'r') as lb:
    try:
        test = (yaml.load(lb))
    except yaml.YAMLError as exc:
        print(exc)
    
maindict = test['device_list']
devices = defaultdict(list)
for location in maindict.values():
    for department in location['department']:
        devices[department].extend(location['devices']) 

print(devices)
print(devices['FIN'])
this could be shortened with list comprehension but now is more explicit


RE: Lists in Dictionaries Assistance - wfsteadman - May-05-2018

Thank You SO MUCH. That worked. And I understood the code as well.

(May-05-2018, 04:44 PM)buran Wrote: here is more general example that will create a dict with devices for each department
note that in your data 'department' is a list, even with one value. I don't know if it is possible to have more than one department in this list

import yaml
from collections import defaultdict
     
with open('router.cfg', 'r') as lb:
    try:
        test = (yaml.load(lb))
    except yaml.YAMLError as exc:
        print(exc)
    
maindict = test['device_list']
devices = defaultdict(list)
for location in maindict.values():
    for department in location['department']:
        devices[department].extend(location['devices']) 

print(devices)
print(devices['FIN'])
this could be shortened with list comprehension but now is more explicit