Get free balance available in the list

Question:

json data is coming from the remote server with ccxt.

How can I get the current balance of "NEO" in it?

def check_balance(symbol):
  balance = exchange.fetch_balance()
  sonuc = balance['info']['balances']
  print(type(sonuc))
  return check_balance

type = list

[
  {'asset': 'BTC', 'free': '0.00000000', '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'}
]
Asked By: Fatih mzm

||

Answers:

Loop through the list and search for NEO

def find_asset(asset_str, asset_list):
    for asset in asset_list:
        if asset['asset'] == asset_str:
            return asset

print(find_asset("NEO", my_list))
Answered By: Alexander Riedel

Something like this:

for i in sonuc:
  if i['asset'] == 'NEO':
    print(i['free'],i['locked'])
Answered By: Patrik

Here is one liner for that

neo_balance = next((sub for sub in source if sub['asset'] == 'NEO'), None)
Answered By: Talha Junaid
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.