Home > Back-end >  How to collect a value from a 3 key in a dictionary?
How to collect a value from a 3 key in a dictionary?

Time:06-14

I have the below dictionary from some API code.

I don't have much of details other the code for getting the data. But I can't figure out how it is possible to collect the 'price' if the 'direction' is BUY and 'trading_pair_id' is 48 and 'status' is 'FILLED'? So I'm hoping that someone could help me. :)

{'count': 9,
 'data': [{'amount': '0.1',
           'customer_order_id': '109317257',
           'date': 1654517494009,
           'direction': 'BUY',
           'id': 109317257,
           'portfolio_id': 1,
           'price': '0.1',
           'remaining_amount': '0.1',
           'status': 'CANCELLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [],
           'trading_pair_id': 1,
           'type': 'LIMIT'},
          {'amount': '1',
           'customer_order_id': '107613290',
           'date': 1655130347003,
           'direction': 'BUY',
           'id': 107613290,
           'portfolio_id': 1,
           'price': '0.1200',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 336335,
                       'price': '0.12',
                       'time': 1655130347003,
                       'value': '1'}],
           'trading_pair_id': 48,
           'type': 'LIMIT'},
          {'amount': '40',
           'customer_order_id': '103699645',
           'date': 1651665830607,
           'direction': 'BUY',
           'id': 103699645,
           'portfolio_id': 1,
           'price': '0.1414',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 179691,
                       'price': '0.141',
                       'time': 1651665830607,
                       'value': '40'}],
           'trading_pair_id': 48,
           'type': 'MARKET'},
          {'amount': '5000',
           'customer_order_id': '103655679',
           'date': 1651585314315,
           'direction': 'BUY',
           'id': 103655679,
           'portfolio_id': 1,
           'price': '0.1302',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 174070,
                       'price': '0.1289',
                       'time': 1651585314315,
                       'value': '5000'}],
           'trading_pair_id': 48,
           'type': 'MARKET'},
          {'amount': '1',
           'customer_order_id': '103655666',
           'date': 1651585258788,
           'direction': 'BUY',
           'id': 103655666,
           'portfolio_id': 1,
           'price': '0.1302',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 174068,
                       'price': '0.1289',
                       'time': 1651585258788,
                       'value': '1'}],
           'trading_pair_id': 48,
           'type': 'MARKET'},
          {'amount': '0.003',
           'customer_order_id': '103232017',
           'date': 1651007532982,
           'direction': 'SELL',
           'id': 103232017,
           'portfolio_id': 1,
           'price': '549780.00',
           'remaining_amount': '0.000',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 137448,
                       'price': '561000',
                       'time': 1651007532982,
                       'value': '0.003'}],
           'trading_pair_id': 50,
           'type': 'MARKET'}],
 'limit': 10,
 'offset': 0}

CodePudding user response:

You can simply use list comprehension,

[entry['price'] for entry in api_response['data'] if entry['direction'] == 'BUY' and entry['status'] == 'FILLED' ...]

Or,

for entry in api_response['data']:
    if entry['direction'] == 'BUY' and entry['status'] == 'FILLED' and entry['trading_pair_id'] == 48:
        do_something_with(entry['price'])
        

if you prefer the other way

CodePudding user response:

d = {'count': 9,
 'data': [{'amount': '0.1',
           'customer_order_id': '109317257',
           'date': 1654517494009,
           'direction': 'BUY',
           'id': 109317257,
           'portfolio_id': 1,
           'price': '0.1',
           'remaining_amount': '0.1',
           'status': 'CANCELLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [],
           'trading_pair_id': 1,
           'type': 'LIMIT'},
          {'amount': '1',
           'customer_order_id': '107613290',
           'date': 1655130347003,
           'direction': 'BUY',
           'id': 107613290,
           'portfolio_id': 1,
           'price': '0.1200',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 336335,
                       'price': '0.12',
                       'time': 1655130347003,
                       'value': '1'}],
           'trading_pair_id': 48,
           'type': 'LIMIT'},
          {'amount': '40',
           'customer_order_id': '103699645',
           'date': 1651665830607,
           'direction': 'BUY',
           'id': 103699645,
           'portfolio_id': 1,
           'price': '0.1414',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 179691,
                       'price': '0.141',
                       'time': 1651665830607,
                       'value': '40'}],
           'trading_pair_id': 48,
           'type': 'MARKET'},
          {'amount': '5000',
           'customer_order_id': '103655679',
           'date': 1651585314315,
           'direction': 'BUY',
           'id': 103655679,
           'portfolio_id': 1,
           'price': '0.1302',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 174070,
                       'price': '0.1289',
                       'time': 1651585314315,
                       'value': '5000'}],
           'trading_pair_id': 48,
           'type': 'MARKET'},
          {'amount': '1',
           'customer_order_id': '103655666',
           'date': 1651585258788,
           'direction': 'BUY',
           'id': 103655666,
           'portfolio_id': 1,
           'price': '0.1302',
           'remaining_amount': '0',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 174068,
                       'price': '0.1289',
                       'time': 1651585258788,
                       'value': '1'}],
           'trading_pair_id': 48,
           'type': 'MARKET'},
          {'amount': '0.003',
           'customer_order_id': '103232017',
           'date': 1651007532982,
           'direction': 'SELL',
           'id': 103232017,
           'portfolio_id': 1,
           'price': '549780.00',
           'remaining_amount': '0.000',
           'status': 'FILLED',
           'stop_price': None,
           'time_in_force': 'GTC',
           'trades': [{'id': 137448,
                       'price': '561000',
                       'time': 1651007532982,
                       'value': '0.003'}],
           'trading_pair_id': 50,
           'type': 'MARKET'}],
 'limit': 10,
 'offset': 0}

for i in d['data']:
    if(i['direction']=='BUY' and i['trading_pair_id'] == 48 and i['status'] == 'FILLED'):
        print(i['price'])
  • Related