0
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?

akkapolk
  • 554
  • 8
  • 33

1 Answers1

1

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
PacketLoss
  • 5,561
  • 1
  • 9
  • 27