Home > Back-end >  Loop over nested dicts
Loop over nested dicts

Time:10-17

Suppose the following dict structure:

{'1': {'Deal_No': '2670034020',
  'Company': 'Test Corp.',
  'Date Low': '01.09.2014',
  'Date High': '03.09.2014',
  'SOURCE': [{'Apple': 0}, //Three items
   {'Samsung': 0},
   {'Sony': 0}],
  'CONTACT': [{'Apple': 0}, //Three items
   {'Samsung': 0},
   {'Sony': 0}],
  'FIRST': [{'Apple': 1}, //Three items
   {'Samsung': 0},
   {'Sony': 0}],
  'ABOUT': [{'Apple': 1}, //Three items
   {'Samsung': 0},
   {'Sony': 0}]},
 '2': {'Deal_No': '2642986020',
  'Company': 'Super Inc.',
  'Date Low': '02.06.2014',
  'Date High': '04.06.2014',
  'SOURCE': [{'Ford': 0}], //One items
  'CONTACT': [{'Ford': 1}], //One items
  'FIRST': [{'Ford': 0}], //One items
  'ABOUT': [{'Ford': 1}]}} //One items

Each outter dict (i.e., "1", "2"...) has another dict as it's value whereas the dict keys "SOURCE", "CONTACT", "FIRST" and "ABOUT" again have a list of dicts as their values. The values for the dicts in the lists can either be 1 or 0. The keys for the dicts in the list are always the same within an outter dict.

Goal: I want to loop over each outter dict and store the keys which all have 0 as value across the inner dicts "SOURCE", "CONTACT", "FIRST" and "ABOUT". Example: In the first outter dict with the key "1", the key "Samsung" has a value of 0 within every one of the four dicts, so I want to store the key "Samsung" in a list. Same goes for the key "Sony". On the other hand, "Apple" should not appear in that list as it has at least one non-zero value within the four dicts.

CodePudding user response:

Try:

dct = {
    "1": {
        "Deal_No": "2670034020",
        "Company": "Test Corp.",
        "Date Low": "01.09.2014",
        "Date High": "03.09.2014",
        "SOURCE": [{"Apple": 0}, {"Samsung": 0}, {"Sony": 0}],
        "CONTACT": [{"Apple": 0}, {"Samsung": 0}, {"Sony": 0}],
        "FIRST": [{"Apple": 1}, {"Samsung": 0}, {"Sony": 0}],
        "ABOUT": [{"Apple": 1}, {"Samsung": 0}, {"Sony": 0}],
    },
    "2": {
        "Deal_No": "2642986020",
        "Company": "Super Inc.",
        "Date Low": "02.06.2014",
        "Date High": "04.06.2014",
        "SOURCE": [{"Ford": 0}],
        "CONTACT": [{"Ford": 1}],
        "FIRST": [{"Ford": 0}],
        "ABOUT": [{"Ford": 1}],
    },
}

keys = ["SOURCE", "CONTACT", "FIRST", "ABOUT"]

out = {}
for k, v in dct.items():
    tmp = {}
    for kk in keys:
        for d in v[kk]:
            tmp.setdefault(list(d)[0], []).append(list(d.values())[0])
    for kk, vv in tmp.items():
        if all(i == 0 for i in vv):
            out.setdefault(k, []).append(kk)

print(out)

Prints:

{"1": ["Samsung", "Sony"]}

CodePudding user response:

I'd just use a for loop.

for key in your_dict:
    print(key)

Get's you each of the keys in your outer dict. The next layer is accessed by using

for key in your_dict:
    for inner_key in your_dict[key]:
        print(your_dict[key][inner_key])

Though I'm sure there are better options.

  • Related