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