0

Here is an example:

This is a JSON file with some data in it:

{
    "planet": {
        "earth": {
            "male": {
                "amount": 9,
                "young": 6,
                "old": 3
            },
            "female": {
                "amount": 12,
                "young": 8,
                "old": 4
            }
        }
       
    }
}

Too many brackets but do not let that intimidate you. If I use the following code:

for item in data["earth"]["gender"]["male"]:
    print(item)

I get this output which is expected:

amount
young
old

but what happened to the values? like, "amount" should be 9 and "young" should be 6 too. So while iterating through the dictionary, we printed ITEM which was "amount", "young" and "old". But if i wanted the output to look like this instead:

amount
9
young
6
old
3

What would we do? Can we do something like this? :

for item in data["earth"]["gender"]["male"]:
    print(item)
    print(item.value)

How do I grab the items and keys of the dictionary? Would appreciate some help, Thanks

Hyperba
  • 79
  • 7
  • Do the answers to this [question](https://stackoverflow.com/questions/57114920/boost-python-3-iterate-over-dict) help at all? – quamrana Apr 16 '22 at 18:16

0 Answers0