akkapolk
akkapolk

Reputation: 594

Get data from list of API response

price_list = [{'symbol': 'ETHBTC', 'lastPrice': '0.03574700'}, {'symbol': 'BTCUSDT', 'lastPrice': '57621.08000000'}]

print(price_list[1]['lastPrice']) # index = 1 for BTCUSDT, print 57621.08000000 => OK.

I need to get lastPrice for BTCUSDT.
Currently I can get it by index.
However, is it possible to get it by referring symbol?

Upvotes: 0

Views: 71

Answers (1)

PacketLoss
PacketLoss

Reputation: 5746

Given you only have two keys which equate to identifier and value.

You could create a dict and flatten your price_list into a key value pair of symbol: lastPrice making it much easier to access the price you need via dict[symbol]

data = {k['symbol']: float(k['lastPrice']) for k in price_list}

data
#{'ETHBTC': 0.035747, 'BTCUSDT': 57621.08}

data['BTCUSDT']
#57621.08

Upvotes: 1

Related Questions