Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python array dictionary inside dictionary

I have python dictionary the code:

{'code': 200,
 'msg': '',
 'snapshotVos': [{'data': {'balances': [{'asset': 'ADD',
                                         'free': '10',
                                         'locked': '0'},
                                        {'asset': 'RDP',
                                         'free': '0',
                                         'locked': '0'},
                                        {'asset': 'SHIB',
                                         'free': '0',
                                         'locked': '947415'}],
                           'totalAsset': '152'},
                  'type': 'spot',
                  'updateTime': 1703807999000}]}

want to take the object values from ‘balances’

I try code but just able to give the ‘snapshotVos’ and ‘data’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need result for all value in array ‘balances’.

need keys from ‘asset’, ‘free’, ‘locked’
and if any array has 0 in the ‘free’ and ‘locked’ remove this array.

print(repo['snapshotVos'])
rows = []
for re in repo['snapshotVos']:
    typeOrder = re['data']
    rows.append(typeOrder)
print(rows)
rows1 = []
for re1 in rows:
    typeOrder = re1['balances'][0]
    rows1.append(typeOrder)
print(rows1)

the result

>>> 200
>>> [{'type': 'spot', 'updateTime': 1703807999000, 'data': {'totalAssetOfBtc': '0.00021152', 'balances': [{'asset': 'ADD', 'free': '10', 'locked': '0'}, {'asset': 'ADXOLD', 'free': '0', 'locked': '0'}]}}]
>>> [{'totalAssetOfBtc': '0.00021152', 'balances': [{'asset': 'ADD', 'free': '10', 'locked': '0'}, {'asset': 'ADXOLD', 'free': '0', 'locked': '0'}]}]
>>>  [{'asset': 'ADD', 'free': '10', 'locked': '0'}]

>Solution :

Add another nested loop:

for re1 in rows:
    for balance in re1['balances']:
        if not (balance['free'] == '0' and balance['locked'] == '0'):
            rows1.append(list(balance.values()))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading