May-05-2018, 04:44 PM
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
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
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs