Python how to parse a list[dict] in python and convert values from nested dictionaries as keys

Naomi

Need help in writing clean code , I have a yaml parsed output which looks like this :

yaml_output = [{'name' : 'alex', 'subjects' : {'maths' : ['grade_1', 'grade_2']}},
               {'name' : 'rio', 'subjects' : {'maths' : ['grade_3', 'grade_2'], 'science : ['grade_4', 'grade_6']}}]

I want it create a list of dictionaries with subjects as key and name of the students as value in a dictionary where grades are the keys.

desired_output = [{'maths' : {'grade_1' : ['alex'], 'grade_2' : ['alex', 'rio'], 'grade_3' : ['rio']}}, {'science' : {'grade_4' : ['rio'], 'grade_6' : ['rio']}

needs subjects as key and name of the students as value in a dictionary where grades are the keys.

new_dict = []
for dict in yaml_output:
  for k,v in dict:
    for i,j in dict['subjects']:
      if any(i in dict_list for dict_list in new_dict):
        dict_list[i].append(v)
Tim Roberts

You were close. Your for k,v loop is looking at the wrong data. You don't want to look at ALL the keys, you want to unravel the subjects key and reference the "name" specifically.

yaml_output = [{'name' : 'alex', 'subjects' : {'maths' : ['grade_1', 'grade_2']}},
               {'name' : 'rio', 'subjects' : {'maths' : ['grade_3', 'grade_2'], 'science': ['grade_4', 'grade_6']}}]

out = dict()
for data in yaml_output:
    for k,v in data['subjects'].items():
        if k not in out:
            out[k] = {}
        for g in v:
            if g not in out[k]:
                out[k][g] = []
            out[k][g].append( data['name'] )
print(out)

Output:

{'maths': {'grade_1': ['alex'], 'grade_2': ['alex', 'rio'], 'grade_3': ['rio']}, 'science': {'grade_4': ['rio'], 'grade_6': ['rio']}}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert the keys of Python dictionary to string for all nested dictionaries too

How to combine (append values) two nested dictionaries with the same keys in python?

Compare keys and values of nested dictionaries in Python

python: combine two nested dictionaries with dictionaries as values of the top level keys

How to add values of keys in dictionaries from input in python

How to extract duplicate keys and values from a list of python dictionaries?

How to create nested dictionaries with duplicate keys in python

Python setting values for keys in nested dictionaries when keys are the same

how to parse values (iterate) from an api, within a list of dictionaries python

Comparing values from nested dictionaries in Python 3

Python: Select keys in nested dictionaries

Sort a list of nested dictionaries by values without knowing its keys [Python]

How to merge two nested dictionaries in python, which have same keys and have array type values?

How to print all the keys and values together from a nested dictionary in Python?

Comparing keys and values in python dictionaries

Recursing python dictionaries with yield from to generate list of nested dictionary keys

Convert Nested dictionaries to tuples python

Python how to sort a list of nested dictionaries by dictionary keys?

Python - Nested Dictionaries - Extracting Values

Python: Nested dictionaries: getting string(key+value) as values then swap with keys, combining dictionaries

Python get multiple specific keys and values from list of dictionaries

Variable from a Class to a Dictionaries keys and values in to Function in Python

Python: Reading a file and adding keys and values to dictionaries from different lines

how to access values of nested dictionary keys in python?

How to convert a dictionary with keys of type of tuple to a list of dictionaries in python?

Using regex to parse out values from dictionaries and count (Python)

How to write Python dictionaries to CSV when the order of keys/values varies?

How to merge two dictionaries based on their keys and values in Python?

Python 3 Nested try exceptions for keys in dictionaries