0

I am new to python programming and I am getting my hands dirty by working on a pet project.

I have tried a lot to avoid these nested for loops, but no success.

Avoiding nested for loops

Returns values from a for loop in python

import requests
import json

r = requests.get('https://api.coinmarketcap.com/v1/ticker/')
j = r.json()


for item in j:
    item['id']
    n = item['id']
    url = 'https://api.coinmarketcap.com/v1/ticker/%s' %n
    req = requests.get(url)
    js = req.json()
    for cool in js:
        print n
        print cool['rank']

Please let me know if more information is needed.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Master
  • 3
  • 2
  • 4
    Sometimes you need nested for loops. This looks like one of those times. – mechanical_meat Jun 09 '17 at 16:13
  • I am getting a lot of data and having nested for loop is too time consuming :( I understand your point though. Thank you for the feedback. – Master Jun 09 '17 at 16:20
  • To avoid writing a nested loop, you could create a function to parse `js` that contains the inner for loop. Note that this would still technically contain nested loops, but your code will at least appear flatter. – Jared Goguen Jun 09 '17 at 16:20
  • 2
    @Master The reason of getting rid of nested for-loop is usually to make it more readable or easier to maintain. It will seldom improve speed. – kennytm Jun 09 '17 at 16:21
  • @Master Are you requesting the same `url` repeatedly? It looks like the only way to reduce complexity/time would be to reduce the number of requests. If each request is unique, I'm not sure how you could do that. – Jared Goguen Jun 09 '17 at 16:21
  • @JaredGoguen yes, I am appending the url with values I get and requesting the new appended url to get actual data. – Master Jun 09 '17 at 16:26

2 Answers2

1

Question

I have too many loops in loops and want a python way of cleaning it up

Answer

Yes, there is a python way of cleaning up loops-in-loops to make it look better but there will still be loops-in-loops under-the-covers.

import requests
import json

r = requests.get('https://api.coinmarketcap.com/v1/ticker/')
j = r.json()

id_list = [item['id'] for item in j]

for n in id_list:
    url = 'https://api.coinmarketcap.com/v1/ticker/%s' %n
    req = requests.get(url)
    js = req.json()
    print "\n".join([ n+"\n"+item['rank'] for item in js ])

Insight from running this

After running this specific code, I realize that your are actually first retrieving the list of tickers in order of rank using

r = requests.get('https://api.coinmarketcap.com/v1/ticker/')

and then using

url = 'https://api.coinmarketcap.com/v1/ticker/%s' %n

to get the rank.

So long as the https://api.coinmarketcap.com/v1/ticker/ continues to return the items in order of rank you could simplify your code like so

import requests
import json

r = requests.get('https://api.coinmarketcap.com/v1/ticker/')
j = r.json()

id_list = [item['id'] for item in j]

result = zip(id_list,range(1,len(id_list)+1) )

for item in result :
print item[0]
print item[1]

Answer to addition question

Addition question : What if I want one more parameter say price_usd? ..... for cool in js: print n print cool['rank'] print cool['price_usd']

Answer :

change the line

print "\n".join([ n+"\n"+item['rank'] for item in js ])

to

print "\n".join([ n+"\n"+item['rank']+"\n"+cool['price_usd'] for item in js ])
Anton Codes
  • 3,663
  • 1
  • 19
  • 28
0

Your first request already gets you everything you need.

import requests
import json

response = requests.get('https://api.coinmarketcap.com/v1/ticker/')
coin_data = response.json()

for coin in coin_data:
    print coin['id'] # "bitcoin", "ethereum", ...
    print coin['rank'] # "1", "2", ...
    print coin['price_usd'] # "2834.75", "276.495", ...
Jared Goguen
  • 8,772
  • 2
  • 18
  • 36