May-05-2018, 06:22 PM
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