-1

I have this code and i've to get an aswer that returns me the 10 highest numbers per percent_change_24h from 'r' variable. What method should i use? I've seen max method but that one returns just one value (the highest sure, but only one)

   url='https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'

params={
    'start':'1',
    'limit':'100',
    'convert':'USD'
}

headers={
    'Accepts':'applications/json',
    'X-CMC_PRO_API_KEY':'b8ee0ea1-ae9b-44ab-9132-02e6e5430eb1'
}

#data= requests.get(url=url,headers=headers,params=params).json()
#pprint(data)`

r= requests.get(url=url,headers=headers,params=params).json()
currencies=[]

for currency in r['data']:
    if currency['quote']['USD']['percent_change_24h']>1:
        currencies.append(
            currency['symbol']
        )

pprint(max(currencies))

2 Answers2

0
from heapq import nlargest

print(nlargest(n, currencies))
Harney
  • 352
  • 1
  • 4
0

Since you have stored the currencies value in a list. You can first arrange it in descending order by: sorted = currencies.sort(reverse=True) Then below will give your N higest value from your list. print(sorted[-N:])

Lakpa Tamang
  • 408
  • 1
  • 4
  • 12