Search code examples
pythonbotspycryptocryptoapi

How the get N highest numbers?


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))

Solution

  • from heapq import nlargest
    
    print(nlargest(n, currencies))