Python Forum
Lists in Dictionaries Assistance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists in Dictionaries Assistance
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing items from 2 lists of dictionaries illwill 7 877 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Searching through Nested Dictionaries and Lists Dave_London 1 705 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  Finding value in nested dictionaries with lists mart79 16 2,823 Mar-08-2020, 08:16 PM
Last Post: ndc85430
  Nested Dictionaries with Lists BriHaug 1 1,343 Feb-23-2019, 02:10 AM
Last Post: ichabod801
  Help with Dictionaries and List of Lists artblinked 7 1,471 Feb-14-2019, 05:07 PM
Last Post: artblinked
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 1,689 Aug-11-2018, 06:45 PM
Last Post: barles

Forum Jump:

User Panel Messages

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