How do I extract a dictionary from a list which only has that dictionary in it?

57 Views Asked by At

I'm trying to make an algorithmic trading program, and

open_positions = trader.open_positions
for position in open_positions:
    print(position)

outputs two dictionaries (?)

{'t': 1, 'ratePrecision': 5, 'tradeId': '32572646', 'accountName': '05654022', 'accountId': '5654022', 'roll': 0, 'com': 0, 'open': 0.71538, 'valueDate': '', 'grossPL': 1298.32056, 'close': 0.71631, 'visiblePL': 9.3, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': True, 'amountK': 1000, 'currencyPoint': 139.59559, 'time': '10022020065344', 'usedMargin': 2500, 'OpenOrderRequestTXT': 'FXTC', 'stop': 0, 'stopMove': 0, 'limit': 0}
{'t': 1, 'ratePrecision': 0, 'tradeId': '', 'accountName': '', 'accountId': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 1298.32056, 'close': 0, 'visiblePL': 9.3, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, 'isTotal': True}

The thing is, when I put the above block of code in my actual program, it falls into a weird loop and never comes out. So, I'm trying to find a way to isolate the real dictionary (the first one) from the list without using a loop. Any help would be greatly appreciated. Thank you very much!

*the list

[{'t': 1, 'ratePrecision': 5, 'tradeId': '32572646', 'accountName': '05654022', 'accountId': '5654022', 'roll': 0, 'com': 0, 'open': 0.71538, 'valueDate': '', 'grossPL': 433.14843, 'close': 0.71569, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': True, 'amountK': 1000, 'currencyPoint': 139.71652, 'time': '10022020065344', 'usedMargin': 2500, 'OpenOrderRequestTXT': 'FXTC', 'stop': 0, 'stopMove': 0, 'limit': 0}, {'t': 1, 'ratePrecision': 0, 'tradeId': '', 'accountName': '', 'accountId': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 433.14843, 'close': 0, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, 'isTotal': True}]


    
1

There are 1 best solutions below

0
On BEST ANSWER

You can do something like this

dict1, dict2 = [{'t': 1, 'ratePrecision': 5, 'tradeId': '32572646', 'accountName': '05654022', 'accountId': '5654022', 'roll': 0, 'com': 0, 'open': 0.71538, 'valueDate': '', 'grossPL': 433.14843, 'close': 0.71569, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': True, 'amountK': 1000, 'currencyPoint': 139.71652, 'time': '10022020065344', 'usedMargin': 2500, 'OpenOrderRequestTXT': 'FXTC', 'stop': 0, 'stopMove': 0, 'limit': 0}, {'t': 1, 'ratePrecision': 0, 'tradeId': '', 'accountName': '', 'accountId': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 433.14843, 'close': 0, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, 'isTotal': True}]

dict1 would then give you:

{'t': 1,
 'ratePrecision': 5,
 'tradeId': '32572646',
 'accountName': '05654022',
 'accountId': '5654022',
 'roll': 0,
 'com': 0,
 'open': 0.71538,
 'valueDate': '',
 'grossPL': 433.14843,
 'close': 0.71569,
 'visiblePL': 3.1,
 'isDisabled': False,
 'currency': 'AUD/USD',
 'isBuy': True,
 'amountK': 1000,
 'currencyPoint': 139.71652,
 'time': '10022020065344',
 'usedMargin': 2500,
 'OpenOrderRequestTXT': 'FXTC',
 'stop': 0,
 'stopMove': 0,
 'limit': 0}

OR you can do something like this:

L = [{'t': 1, 'ratePrecision': 5, 'tradeId': '32572646', 'accountName': '05654022', 'accountId': '5654022', 'roll': 0, 'com': 0, 'open': 0.71538, 'valueDate': '', 'grossPL': 433.14843, 'close': 0.71569, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': True, 'amountK': 1000, 'currencyPoint': 139.71652, 'time': '10022020065344', 'usedMargin': 2500, 'OpenOrderRequestTXT': 'FXTC', 'stop': 0, 'stopMove': 0, 'limit': 0}, {'t': 1, 'ratePrecision': 0, 'tradeId': '', 'accountName': '', 'accountId': '', 'roll': 0, 'com': 0, 'open': 0, 'valueDate': '', 'grossPL': 433.14843, 'close': 0, 'visiblePL': 3.1, 'isDisabled': False, 'currency': 'AUD/USD', 'isBuy': False, 'amountK': 1000, 'currencyPoint': 0, 'time': None, 'usedMargin': 0, 'stop': 0, 'stopMove': 0, 'limit': 0, 'isTotal': True}]

L[0]

{'t': 1,
     'ratePrecision': 5,
     'tradeId': '32572646',
     'accountName': '05654022',
     'accountId': '5654022',
     'roll': 0,
     'com': 0,
     'open': 0.71538,
     'valueDate': '',
     'grossPL': 433.14843,
     'close': 0.71569,
     'visiblePL': 3.1,
     'isDisabled': False,
     'currency': 'AUD/USD',
     'isBuy': True,
     'amountK': 1000,
     'currencyPoint': 139.71652,
     'time': '10022020065344',
     'usedMargin': 2500,
     'OpenOrderRequestTXT': 'FXTC',
     'stop': 0,
     'stopMove': 0,
     'limit': 0}