-1

I'm trying to access some info from a dictionary that sits in a list of dictionaries and I'm really struggling. I'm trying to check my balance on a crypto exchange and when I query it I receive this list of dictionaries:

[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'}, 
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'BNB', 'free': '0.00000000']

All I'm looking to do is be able to select an asset and return the free amount.

I don't have much experience with dictionaries but I was hoping to be able to do something like:

if asset == 'BTC':
    print(free)

With the desired result in this case to have 0.50000000 returned to me.

Obviously I know that's not going to work but I'm not sure what to do.

For what it's worth I'm using the ccxt Crypto wrapper in python, using fetch_balance(), you'd think there would be a paramter that allows you to select what currency you want to check the balance of but there doesn't seem to be. If anyone knows otherwise though that would also be really helpful!

Many thanks

gosuto
  • 5,422
  • 6
  • 36
  • 57
top bantz
  • 585
  • 1
  • 12
  • 29

6 Answers6

2

First notice that you wrote the dictionary with mistake, you need to close it with "}]".

This is a code that would work for your case, it iterate over the dictionary:

dict = [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'}, 
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'BNB', 'free': '0.00000000'}]

for element in dict:
    if element['asset'] == 'BTC':
        print(element['free'])

it prints 0.5

Yagel
  • 1,184
  • 2
  • 18
  • 41
1
for dictionary in list:
     if dictionary['asset'] == 'BTC':
          print(dictionary['free'])

That should loop through the list and print each free value for which asset equals 'BTC'.

Cade
  • 61
  • 2
  • That's exactly what I'm looking for I actually tried this with out the brackets cos I'm an idiot. It's been a long long day! Thanks – top bantz Aug 02 '19 at 21:06
1
import pandas as pd
list_of_dicts = [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'}, 
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'BNB', 'free': '0.00000000'}]

a = pd.DataFrame(list_of_dicts)
res = a.loc[a['asset'] == 'BTC']
res.iloc[0]["free"]     

output:

0.50000000
frankegoesdown
  • 1,898
  • 1
  • 20
  • 38
1
dict = [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'}, 
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'}, 
{'asset': 'BNB', 'free': '0.00000000'}]

def printBTC(asset):
    if asset.asset = 'BTC':
        print(asset.free)

map(printBTC, dict)

The reason that works is that the map function takes a function and will pass each member of the array into the function

1

For example, you could just sum all the free amounts.

In [1]: balance = [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},  
   ...: {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},  
   ...: {'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},  
   ...: {'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},  
   ...: {'asset': 'BNB', 'free': '0.00000000'}]                                                          

In [2]: sum(float(a['free']) for a in balance)                                                           
Out[2]: 0.5

Or create a new dictionary that just has the asset name and free amount. That would be easier to query.

In [3]: info = {a['asset']: a['free'] for a in balance}                                                         
Out[3]: 
{'BTC': '0.50000000',
 'LTC': '0.00000000',
 'ETH': '0.00000000',
 'NEO': '0.00000000',
 'BNB': '0.00000000'}

In [4]: info['BTC']                                                                                      
Out[4]: '0.50000000'
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
1

Pythonic solution:

The pythonic way to do it is using next built-in function:

next( x['free'] for x in your_list_of_dics if x['asset'] == 'BTC' )

Sample:

l=[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'}, 
   {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, 
   {'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}, 
   {'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'}, 
   {'asset': 'BNB', 'free': '0.00000000'}]

result = next( x['free'] for x in l if x['asset'] == 'BTC' )

print(result)

'0.50000000'

Handling execptions:

try:
    l=[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'}, 
       {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, 
       {'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'}, 
       {'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'}, 
       {'asset': 'BNB', 'free': '0.00000000'}]
    result = next( x['free'] for x in l if x['asset'] == 'BTC' )
    print(result)

except StopIteration:
    print ( "No 'BTC' value in any items." )

except KeyError::
    print ( "No 'free' or no 'asset' fields in items." )

It's important to hande exceptions: what happens if any item has 'BTC' value on asset? What happens if some dict don't has 'asset' or 'free' ?

dani herrera
  • 48,760
  • 8
  • 117
  • 177