'How read var in list in dict
How do I get the value of the variable "price" in this compound dictionary?
{"coins":[{"id":"bitcoin","icon":"https://static.coinstats.app/coins/Bitcoin6l39t.png","name":"Bitcoin","symbol":"BTC","rank":1,"price":47281.470468725514}]}
Solution 1:[1]
first item in the list:
>>> coins = {"coins":[{"id":"bitcoin","icon":"https://static.coinstats.app/coins/Bitcoin6l39t.png","name":"Bitcoin","symbol":"BTC","rank":1,"price":47281.470468725514}]}
>>> coins.get('coins')[0].get('price')
47281.470468725514
>>>
Solution 2:[2]
From the dict you shared, you can get the value for key price like this,
d = {"coins":[{"id":"bitcoin","icon":"https://static.coinstats.app/coins/Bitcoin6l39t.png","name":"Bitcoin","symbol":"BTC","rank":1,"price":47281.470468725514}]}
print(d['coins'][0]['price']) # 47281.470468725514
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jeroenflvr |
| Solution 2 | Sreeram TP |
