Python Forum
Python3 for loop over a dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 for loop over a dict
#1
Quote:Hello!
I am trying to get all the values individually for each asset for example:

BCHUSD
a = 301.340000
b = 301.160000
c = 301.280000

TRXUSD
a = 0.0609450
b = 0.0609440
c = 0.0609540 and so on...

Could someone tell me how I can do it please?

Regards!

import requests
import json


while True:    
    req = requests.get('https://api.kraken.com/0/public/Ticker?pair=BCHUSD,TRXUSD,XRPUSD')
    
	print(req)
	
	<Response [200]>
	
    print(type(req))
	
	<class 'requests.models.Response'>
	
    obj = req.json()
    
	print(type(obj))
	
	<class 'dict'>
	
    print(obj)
	
{'error': [], 'result': {'BCHUSD': {'a': ['301.340000', '8', '8.000'], 'b': ['301.160000', '5', '5.000'], 'c': ['301.280000', '2.17601336'], 'v': ['24.29958509', '5601.96455701'], 'p': ['301.518130', '277.596849'], 't': [15, 2308], 'l': ['299.000000', '260.000000'], 'h': ['302.390000', '309.900000'], 'o': '299.000000'}, 
'TRXUSD': {'a': ['0.0609450', '70500', '70500.000'], 'b': ['0.0609440', '1094', '1094.000'], 'c': ['0.0609540', '360.09238846'], 'v': ['19826.64747345', '9636655.93947494'], 'p': ['0.0610639', '0.0589701'], 't': [22, 1735], 'l': ['0.0608850', '0.0562060'], 'h': ['0.0612840', '0.0618410'], 'o': '0.0611130'}, 
'XXRPZUSD': {'a': ['0.69453000', '720', '720.000'], 'b': ['0.69419000', '694', '694.000'], 'c': ['0.69180000', '2196.58617800'], 'v': ['235770.55961560', '28023892.06456976'], 'p': ['0.69880570', '0.66007675'], 't': [415, 10359], 'l': ['0.69121000', '0.62229000'], 'h': ['0.70386000', '0.72105000'], 'o': '0.69935000'}}}
        
    
    for k, v in obj.items():
        if type(v) is dict and k:
            for nk, nv in v.items():
                print(nk, nv)
				
				
BCHUSD {'a': ['298.240000', '11', '11.000'], 'b': ['298.040000', '3', '3.000'], 'c': ['299.000000', '0.89507885'], 'v': ['38.42175237', '5614.56089299'], 'p': ['300.890848', '277.650439'], 't': [24, 2314], 'l': ['299.000000', '260.000000'], 'h': ['302.390000', '309.900000'], 'o': '299.000000'}
TRXUSD {'a': ['0.0608250', '4881', '4881.000'], 'b': ['0.0607820', '40500', '40500.000'], 'c': ['0.0608630', '81.94337742'], 'v': ['21067.61432979', '9622286.56922629'], 'p': ['0.0610566', '0.0589675'], 't': [25, 1729], 'l': ['0.0608630', '0.0562060'], 'h': ['0.0612840', '0.0618410'], 'o': '0.0611130'}
XXRPZUSD {'a': ['0.69018000', '666', '666.000'], 'b': ['0.69000000', '42829', '42829.000'], 'c': ['0.69022000', '358.00000000'], 'v': ['287549.02071579', '27810492.67564827'], 'p': ['0.69737332', '0.65981291'], 't': [429, 10340], 'l': ['0.69000000', '0.62229000'], 'h': ['0.70386000', '0.72105000'], 'o': '0.69935000'}
Reply
#2
OK, so nv there is a dict isn't it? You know how to iterate over a dict.
Reply
#3
You haven't asked a question here. What are you trying to do with req? I would think that if req["error"] is empty that req["result"] will be full of the information you need. What do you want to do with that information?
for currency_tag, currency_info in req["result"].items():
    print(currency_tag)
    for key, value in currency_info.items():
        print(key, value)
I would put this in a dataframe.
req = {
    'error': [],
    'result': {
        'BCHUSD': {
            'a': ['301.340000', '8', '8.000'],
            'b': ['301.160000', '5', '5.000'],
            'c': ['301.280000', '2.17601336'],
            'v': ['24.29958509', '5601.96455701'],
            'p': ['301.518130', '277.596849'],
            't': [15, 2308],
            'l': ['299.000000', '260.000000'],
            'h': ['302.390000', '309.900000'],
            'o': '299.000000'
            }, 
        'TRXUSD': {
            'a': ['0.0609450', '70500', '70500.000'],
            'b': ['0.0609440', '1094', '1094.000'],
            'c': ['0.0609540', '360.09238846'],
            'v': ['19826.64747345', '9636655.93947494'],
            'p': ['0.0610639', '0.0589701'],
            't': [22, 1735],
            'l': ['0.0608850', '0.0562060'],
            'h': ['0.0612840', '0.0618410'],
            'o': '0.0611130'
            }, 
        'XXRPZUSD': {
            'a': ['0.69453000', '720', '720.000'],
            'b': ['0.69419000', '694', '694.000'],
            'c': ['0.69180000', '2196.58617800'],
            'v': ['235770.55961560', '28023892.06456976'],
            'p': ['0.69880570', '0.66007675'],
            't': [415, 10359],
            'l': ['0.69121000', '0.62229000'],
            'h': ['0.70386000', '0.72105000'],
            'o': '0.69935000'
            }
        }
    }

import pandas as pd

df = pd.DataFrame(req["result"])
print(df)
Output:
BCHUSD TRXUSD XXRPZUSD a [301.340000, 8, 8.000] [0.0609450, 70500, 70500.000] [0.69453000, 720, 720.000] b [301.160000, 5, 5.000] [0.0609440, 1094, 1094.000] [0.69419000, 694, 694.000] c [301.280000, 2.17601336] [0.0609540, 360.09238846] [0.69180000, 2196.58617800] v [24.29958509, 5601.96455701] [19826.64747345, 9636655.93947494] [235770.55961560, 28023892.06456976] p [301.518130, 277.596849] [0.0610639, 0.0589701] [0.69880570, 0.66007675] t [15, 2308] [22, 1735] [415, 10359] l [299.000000, 260.000000] [0.0608850, 0.0562060] [0.69121000, 0.62229000] h [302.390000, 309.900000] [0.0612840, 0.0618410] [0.70386000, 0.72105000] o 299.000000 0.0611130 0.69935000
You can do lots of interesting things with a dataframe.
Reply
#4
Or as a function:
dict1 = {'error': [], 'result': {'BCHUSD': {'a': ['301.340000', '8', '8.000'], 'b': ['301.160000', '5', '5.000'], 'c': ['301.280000', '2.17601336'], 'v': ['24.29958509', '5601.96455701'], 'p': ['301.518130', '277.596849'], 't': [15, 2308], 'l': ['299.000000', '260.000000'], 'h': ['302.390000', '309.900000'], 'o': '299.000000'}, 
'TRXUSD': {'a': ['0.0609450', '70500', '70500.000'], 'b': ['0.0609440', '1094', '1094.000'], 'c': ['0.0609540', '360.09238846'], 'v': ['19826.64747345', '9636655.93947494'], 'p': ['0.0610639', '0.0589701'], 't': [22, 1735], 'l': ['0.0608850', '0.0562060'], 'h': ['0.0612840', '0.0618410'], 'o': '0.0611130'}, 
'XXRPZUSD': {'a': ['0.69453000', '720', '720.000'], 'b': ['0.69419000', '694', '694.000'], 'c': ['0.69180000', '2196.58617800'], 'v': ['235770.55961560', '28023892.06456976'], 'p': ['0.69880570', '0.66007675'], 't': [415, 10359], 'l': ['0.69121000', '0.62229000'], 'h': ['0.70386000', '0.72105000'], 'o': '0.69935000'}}}

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1

display_dict(dict1)
Output:
error: [] result BCHUSD a: ['301.340000', '8', '8.000'] b: ['301.160000', '5', '5.000'] c: ['301.280000', '2.17601336'] v: ['24.29958509', '5601.96455701'] p: ['301.518130', '277.596849'] t: [15, 2308] l: ['299.000000', '260.000000'] h: ['302.390000', '309.900000'] o: 299.000000 TRXUSD a: ['0.0609450', '70500', '70500.000'] b: ['0.0609440', '1094', '1094.000'] c: ['0.0609540', '360.09238846'] v: ['19826.64747345', '9636655.93947494'] p: ['0.0610639', '0.0589701'] t: [22, 1735] l: ['0.0608850', '0.0562060'] h: ['0.0612840', '0.0618410'] o: 0.0611130 XXRPZUSD a: ['0.69453000', '720', '720.000'] b: ['0.69419000', '694', '694.000'] c: ['0.69180000', '2196.58617800'] v: ['235770.55961560', '28023892.06456976'] p: ['0.69880570', '0.66007675'] t: [415, 10359] l: ['0.69121000', '0.62229000'] h: ['0.70386000', '0.72105000'] o: 0.69935000
BashBedlam likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop Dict with inconsistent Keys Personne 1 1,607 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Sort a dict in dict cherry_cherry 4 73,336 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,897 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  file.write stops while true loop from executing in python3 boonr 3 3,103 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  dict printing last key,value out of loop anna 6 3,329 Feb-23-2019, 03:22 AM
Last Post: anna
  Code issue with time remaining loop. Python3 deboerdn2000 11 8,809 May-04-2017, 04:53 PM
Last Post: deboerdn2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020