0

I have an API returning a response like this one.

data = [
  {'id': 'x27fujsjfsfjsjf', 'price': '89992',  'type': 'ONE'}, {'id': 'ujufajfjfwau', 'price': '7777',  'type': 'ONE'}, {'id': 'x27adarasda', 'price': '88882',  'type': 'TWO'}
] 

I would like to parse the response in pairs for example id and price. I use the python Websocket client to fetch data in real time.

Right now I use:

 for d in data: 
       print (d['id])

But I can't find a solution to parse both the id,price and type together. Since I'm interested of price for a particular ID.

2 Answers2

0

You can do this.

for d in data:
    print (d['id'], d['price'], d['type'])

You can pass the values in the same way to another function, if you want to use these values somewhere.

A lot more information is available in this SO

aksappy
  • 3,400
  • 3
  • 23
  • 49
0

You just have to 'say' what you want to print from the dictionary:

data = [
        {'id': 'x27fujsjfsfjsjf', 'price': '89992', 'type': 'ONE'},
        {'id': 'ujufajfjfwau', 'price': '7777',  'type': 'ONE'},
        {'id': 'x27adarasda', 'price': '88882',  'type': 'TWO'}
]

for item in data:
    print(item['id']) # this prints out the value of the ID
    print(item['price']) # this prints out the value of the PRICE
    print(item['type']) # this prints out the value of the TYPE
Julio S.
  • 944
  • 1
  • 12
  • 26