Nancy
Nancy

Reputation: 179

Writing a python function to loop out the values of each id

Below is my json data. What I want to do is write a python function to loop out the values of each id. Would anybody please help me with this?

[
    {
        "id": "1", 
        "code": "111", 
    }, 
    {
        "id": "2", 
        "code": "222", 
    }, 
    {
        "id": "3", 
        "code": "333", 
    }
]

Upvotes: 1

Views: 2313

Answers (3)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250981

>>> data=[{"id": "1","code": "111",},{"id": "2","code": "222",},{"id": "3","code": "333",}]
>>> list_id=[x['id'] for x in data]
>>> print(list_id)
['1','2','3']

Upvotes: 1

bengo
bengo

Reputation: 86

If jsondata is a Python list with your example data, you can do:

ids = [item['id'] for item in jsondata]

Upvotes: 2

Simeon Visser
Simeon Visser

Reputation: 122376

If jsondata is a Python list with your example data, you can do:

for item in jsondata:
    print item['id']

Upvotes: 3

Related Questions