like the title says I'm having a problem filtering an array that I'm getting from the CoinGecko API. The array looks like this:
[
{
"id": "01coin",
"symbol": "zoc",
"name": "01coin"
},
{
"id": "0-5x-long-algorand-token",
"symbol": "algohalf",
"name": "0.5X Long Algorand Token"
},
{
"id": "0-5x-long-altcoin-index-token",
"symbol": "althalf",
"name": "0.5X Long Altcoin Index Token"
}
]
After the filter I'd like it to only show the "id"s like this:
[
"01coin",
"0-5x-long-algorand-token",
"0-5x-long-altcoin-index-token"
]
This is how I tried to filter it:
coinList = 'https://api.coingecko.com/api/v3/coins/list'
listCall = requests.get(coinList)
jsonCall = json.loads(listCall.content)
coinIds = [x for x in jsonCall if x == 'id']
CodePudding user response:
Your list comprehension is sort of there, but you should be indexing into each dictionary rather than using an if
clause. It should look like:
[item["id"] for item in jsonCall]
This outputs:
['01coin', '0-5x-long-algorand-token', '0-5x-long-altcoin-index-token']