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.

6
  • 4
    Sometimes you need nested for loops. This looks like one of those times. Commented Jun 9, 2017 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. Commented Jun 9, 2017 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. Commented Jun 9, 2017 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. Commented Jun 9, 2017 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. Commented Jun 9, 2017 at 16:21

2 Answers 2

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 ])
Sign up to request clarification or add additional context in comments.

5 Comments

What if I want one more parameter say price_usd? ..... for cool in js: print cool['price_usd']
I updated the response to have the additional answer.
thank you for the input and explaining a pythonic way to write a code :)
don't say thanks, just upvote and/or accept the answer. cheers
Well I can't upvote because my reputation is too low. Also, Jared Goguen's response saves me a lot of execution time :) But, I appreciate your efforts to help me out.
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", ...

1 Comment

Respect. Appreciate the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.