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


Messages In This Thread
Lists in Dictionaries Assistance - by wfsteadman - May-05-2018, 03:06 PM
RE: Lists in Dictionaries Assistance - by buran - May-05-2018, 04:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,383 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Comparing items from 2 lists of dictionaries illwill 7 2,770 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Searching through Nested Dictionaries and Lists Dave_London 1 7,707 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  Finding value in nested dictionaries with lists mart79 16 8,097 Mar-08-2020, 08:16 PM
Last Post: ndc85430
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,288 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Nested Dictionaries with Lists BriHaug 1 53,226 Feb-23-2019, 02:10 AM
Last Post: ichabod801
  Help with Dictionaries and List of Lists artblinked 7 3,125 Feb-14-2019, 05:07 PM
Last Post: artblinked
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 3,216 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