Jay
Jay

Reputation: 41

Python Dictionary key iteration

Hi Guys im looking to print out a dictionary. I have a 1000 lines i need to print.

The following code gets data in a json. I then put it into a dict. It then prints out the first line or key 0 in Data. I want to print up to line 1000 but i want to do it without typing it a 1000 times :)

import time, json, requests

json_api = requests.get("https://www.cryptopia.co.nz/api/GetMarketHistory/ETN_BTC/72")
json_data = json_api.json()

print(json_data['Data'][0]['Price'] , json_data['Data'][0]['Timestamp'] )

This gives output:

4.35e-06 1510454994

Which is correct.

Now i want to print out results for

print(json_data['Data'][1]['Price'] , json_data['Data'][1]['Timestamp'] )
print(json_data['Data'][2]['Price'] , json_data['Data'][2]['Timestamp'] )
print(json_data['Data'][3]['Price'] , json_data['Data'][3]['Timestamp'] )

But without having to write this a 1000 times. Thanks

Upvotes: 2

Views: 39

Answers (1)

Jacob G.
Jacob G.

Reputation: 29680

Just use a loop!

for i in range(1, 1000):
    print(json_data['Data'][i]['Price'] , json_data['Data'][i]['Timestamp'] )

Upvotes: 1

Related Questions