StillLearningHowtodoit
StillLearningHowtodoit

Reputation: 141

Format list of nested list of dictionaries

I am trying to format a data structure to my need. From a list of dict i want to create a list of dict:

[
    {
        "nameWithOwner": "Repository-Org/documentation",
        "languages": {
            "edges": [
                {
                    "node": {
                        "name": "HCL"
                    },
                    "size": 3366
                },
                {
                    "node": {
                        "name": "Shell"
                    },
                    "size": 1435
                },
                {
                    "node": {
                        "name": "Ruby"
                    },
                    "size": 868
                },
                {
                    "node": {
                        "name": "HTML"
                    },
                    "size": 30650
                },
                {
                    "node": {
                        "name": "CSS"
                    },
                    "size": 59692
                }
            ]
        }
    },
    {
        "nameWithOwner": "Repository-Org/integration-api",
        "languages": {
            "edges": [
                {
                    "node": {
                        "name": "JavaScript"
                    },
                    "size": 245648
                },
                {
                    "node": {
                        "name": "Python"
                    },
                    "size": 7483
                },
                {
                    "node": {
                        "name": "Shell"
                    },
                    "size": 20050
                },
                {
                    "node": {
                        "name": "Dockerfile"
                    },
                    "size": 1343
                },
                {
                    "node": {
                        "name": "Roff"
                    },
                    "size": 492
                }
            ]
        }
    }
        {
        "nameWithOwner": "Repository-Org/demo",
        "languages": {
            "edges": [
                {
                    "node": {
                        "name": "Jupyter Notebook"
                    },
                    "size": 5125
                },
                {
                    "node": {
                        "name": "Python"
                    },
                    "size": 8623
                }
            ]
        }
    },
    {
        "nameWithOwner": "Repository-Org/github-link",
        "languages": {
            "edges": []
        }
    },
    {
        "nameWithOwner": "Repository-Org/intelligence-config",
        "languages": {
            "edges": []
        }
    }
]

I want to go though it and format it to get a list of dictionary like this, I didn't find a way to access the nested data or understand if it's possible or not to do it

 [
    {
        "name": "Repository-Org/documentation",
        "language": {
            "HCL": 3366,
            "Shell": 1435,
            "Ruby": 868,
            "HTML": 30650,
            "CSS": 59692
        }
    },
    {
        "name": "Repository-Org/integration-api",
        "language": {
            "JavaScript": 245648,
            "Python": 7483,
            "Shell": 20050,
            "Dockerfile": 1343,
            "Roff": 492
        }
    },
    {
        "name": "Repository-Org/demo",
        "language": {
            "Jupyter Notebook": 5125,
            "Python": 8623
        }
    },
    {
        "name": "Repository-Org/github-link",
        "language": {}
    },
    {
        "name": "Repository-Org/intelligence-config",
        "language": {}
    },
]

At the moment i did this :

dicta = {}
array = []
for repository in variables:
    for key,value in repository.items() :
        dicta[key]=value
    array.append(dicta)
    print(array)

And got this result :

[
    {'nameWithOwner': 'Repository-Org/documentation', 'languages': {'edges': [
                {'node': {'name': 'HCL'
                    }, 'size': 3366
                },
                {'node': {'name': 'Shell'
                    }, 'size': 1435
                },
                {'node': {'name': 'Ruby'
                    }, 'size': 868
                },
                {'node': {'name': 'HTML'
                    }, 'size': 30650
                },
                {'node': {'name': 'CSS'
                    }, 'size': 59692
                }
            ]
        }
    }
]
[
    {'nameWithOwner': 'Repository-Org/integration-api', 'languages': {'edges': [
                {'node': {'name': 'JavaScript'
                    }, 'size': 245648
                },
                {'node': {'name': 'Python'
                    }, 'size': 7483
                },
                {'node': {'name': 'Shell'
                    }, 'size': 20050
                },
                {'node': {'name': 'Dockerfile'
                    }, 'size': 1343
                },
                {'node': {'name': 'Roff'
                    }, 'size': 492
                }
            ]
        }
    },
    {'nameWithOwner': 'Repository-Org/integration-api', 'languages': {'edges': [
                {'node': {'name': 'JavaScript'
                    }, 'size': 245648
                },
                {'node': {'name': 'Python'
                    }, 'size': 7483
                },
                {'node': {'name': 'Shell'
                    }, 'size': 20050
                },
                {'node': {'name': 'Dockerfile'
                    }, 'size': 1343
                },
                {'node': {'name': 'Roff'
                    }, 'size': 492
                }
            ]
        }
    }
]
[
    {'nameWithOwner': 'Repository-Org/demo', 'languages': {'edges': [
                {'node': {'name': 'Jupyter Notebook'
                    }, 'size': 5125
                },
                {'node': {'name': 'Python'
                    }, 'size': 8623
                }
            ]
        }
    },
    {'nameWithOwner': 'Repository-Org/demo', 'languages': {'edges': [
                {'node': {'name': 'Jupyter Notebook'
                    }, 'size': 5125
                },
                {'node': {'name': 'Python'
                    }, 'size': 8623
                }
            ]
        }
    },
    {'nameWithOwner': 'Repository-Org/demo', 'languages': {'edges': [
                {'node': {'name': 'Jupyter Notebook'
                    }, 'size': 5125
                },
                {'node': {'name': 'Python'
                    }, 'size': 8623
                }
            ]
        }
    }
]
[
    {'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
        }
    },
    {'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
        }
    },
    {'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
        }
    },
    {'nameWithOwner': 'Repository-Org/github-link', 'languages': {'edges': []
        }
    }
]
[
    {'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
        }
    },
    {'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
        }
    },
    {'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
        }
    },
    {'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
        }
    },
    {'nameWithOwner': 'Repository-Org/intelligence-config', 'languages': {'edges': []
        }
    }
]

What should i change to get what I want ?

You can find a working example here : https://repl.it/repls/FastAlphanumericKnowledge

Upvotes: 1

Views: 63

Answers (2)

ex4
ex4

Reputation: 2428

Try like this


reps = {}

for x in origdata:
  if not x["nameWithOwner"] in reps:
    reps[x["nameWithOwner"]]={}
  for y in x["languages"]["edges"]:
    reps[x["nameWithOwner"]][y["node"]["name"]] = y["size"]

res = []
for x in reps:
  res.append({"name":x, "language":reps[x]})

# pprint for nice output
from pprint import pprint
pprint(res) 


Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98861

You can try:

original = [
    {
        "nameWithOwner": "Repository-Org/documentation",
        "languages": {
            "edges": [
                {
                    "node": {
                        "name": "HCL"
                    },
                    "size": 3366
                },
                {
                    "node": {
                        "name": "Shell"
                    },
                    "size": 1435
                },
                {
                    "node": {
                        "name": "Ruby"
                    },
                    "size": 868
                },
                {
                    "node": {
                        "name": "HTML"
                    },
                    "size": 30650
                },
                {
                    "node": {
                        "name": "CSS"
                    },
                    "size": 59692
                }
            ]
        }
    },
    {
        "nameWithOwner": "Repository-Org/integration-api",
        "languages": {
            "edges": [
                {
                    "node": {
                        "name": "JavaScript"
                    },
                    "size": 245648
                },
                {
                    "node": {
                        "name": "Python"
                    },
                    "size": 7483
                },
                {
                    "node": {
                        "name": "Shell"
                    },
                    "size": 20050
                },
                {
                    "node": {
                        "name": "Dockerfile"
                    },
                    "size": 1343
                },
                {
                    "node": {
                        "name": "Roff"
                    },
                    "size": 492
                }
            ]
        }
    },
    {
        "nameWithOwner": "Repository-Org/demo",
        "languages": {
            "edges": [
                {
                    "node": {
                        "name": "Jupyter Notebook"
                    },
                    "size": 5125
                },
                {
                    "node": {
                        "name": "Python"
                    },
                    "size": 8623
                }
            ]
        }
    },
    {
        "nameWithOwner": "Repository-Org/github-link",
        "languages": {
            "edges": []
        }
    },
    {
        "nameWithOwner": "Repository-Org/intelligence-config",
        "languages": {
            "edges": []
        }
    }
]
new_dicts = []
for d in original:
    temp_dict = {}
    temp_dict['name'] = d['nameWithOwner']
    temp_dict['languages'] = {}
    for l in d['languages']['edges']:
        temp_dict['languages'][l['node']['name']] = l['size']

    new_dicts.append(temp_dict)

print(new_dicts)

[{'name': 'Repository-Org/documentation', 'languages': {'HCL': 3366, 'Shell': 1435, 'Ruby': 868, 'HTML': 30650, 'CSS': 59692}}, {'name': 'Repository-Org/integration-api', 'languages': {'JavaScript': 245648, 'Python': 7483, 'Shell': 20050, 'Dockerfile': 1343, 'Roff': 492}}, {'name': 'Repository-Org/demo', 'languages': {'Jupyter Notebook': 5125, 'Python': 8623}}, {'name': 'Repository-Org/github-link', 'languages': {}}, {'name': 'Repository-Org/intelligence-config', 'languages': {}}]

Demo

Upvotes: 1

Related Questions