ArunPrasath
ArunPrasath

Reputation: 183

I would like to iterate over multiple array list within the dictionary and print each key and value

I would like to iterate through a multiple array list within the dictionary. Please suggest.

Provided the code that i have tried. Below is the dictionary, that i need to iterate through and print key and value.

{
    'page': 2, 
    'per_page': 6, 
    'total': 12, 
    'total_pages': 2, 
    'data': [
        {
            'id': 7, 
            'email': '[email protected]', 
            'first_name': 'Michael', 
            'last_name': 'Lawson', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg'
        }, 
        {
            'id': 8, 
            'email': '[email protected]', 
            'first_name': 'Lindsay', 
            'last_name': 'Ferguson', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg'
        }, 
        {
            'id': 9, 
            'email': '[email protected]', 
            'first_name': 'Tobias', 
            'last_name': 'Funke', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg'
        }, 
        {
            'id': 10, 
            'email': '[email protected]', 
            'first_name': 'Byron', 
            'last_name': 'Fields', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg'
        }, 
        {
            'id': 11, 
            'email': '[email protected]', 
            'first_name': 'George', 
            'last_name': 'Edwards', 
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg'
        }, 
        {
            'id': 12,
            'email': '[email protected]', 4
            'first_name': 'Rachel', 
            'last_name': 'Howell',
            'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg'
        }
    ]
}

My code:

import json


res = requests.get('https://reqres.in/api/users?page=2')
data = res.content
print(type(data))

jsondata = json.loads(data)
print(type(jsondata))  # Turning JSON encoded data into Python objects.

jsondata = json.dumps(jsondata, indent=4)  # Serialize the json data
print(type(jsondata))
print(jsondata)

res1 = requests.get('https://reqres.in/api/users?page=2')
data1 = res1.content
print(data1)

jsondata1 = json.loads(data1)
jsondata1 = json.dumps(jsondata1)
print(jsondata1)

for key, value in jsondata.items():
    print(key, '->', value)

From the dictionary, we need to print all,

Key 
Value

Upvotes: 1

Views: 119

Answers (1)

Hetal Thaker
Hetal Thaker

Reputation: 717

import json
import requests
def printseq(seq):
    if (isinstance(seq,dict)):
        for key,values in seq.items():
            if (isinstance(values,list)):
                for i in values:
                    if (isinstance(i,dict)):
                        printseq(i)
            else:
                print(key,"->",values)
res=requests.get('https://reqres.in/api/users?page=2')
data=res.content
jsondata = json.loads(data)
printseq(jsondata)

Try this out, here in this code recursive procedure is used to print dictionary. Before printing value isinstance() method is used to check the type of value and if it is dictionary recursion is called.

Upvotes: 2

Related Questions