1

I am trying to split the list into number of dictonaries that are contained inside that list....Below is an example of a list containing 3 dictonaries - CODE :

def open_orders_order(self,ticker):
        filter = json.dumps({"open": True})
        column = 'orderQty'
        open = client.Order.Order_getOrders(symbol=ticker, filter=filter, count=100, reverse=True).result()
        print(open[0])

This results in the OUTPUT:

[{'orderID': 'eb5ba1c3-3506-a55e7-5fe7-fa6e654933a7', 'clOrdID': '', 'clOrdLinkID': '', 'account': 24565, 'symbol': 'XBTUSD', 'side': 'Buy', 'simpleOrderQty': None, 'orderQty': 10, 'price': 2900.0, 'displayQty': None, 'stopPx': None, 'pegOffsetValue': None, 'pegPriceType': '', 'currency': 'USD', 'settlCurrency': 'XBt', 'ordType': 'Limit', 'timeInForce': 'GoodTillCancel', 'execInst': '', 'contingencyType': '', 'exDestination': 'XBME', 'ordStatus': 'New', 'triggered': '', 'workingIndicator': True, 'ordRejReason': '', 'simpleLeavesQty': None, 'leavesQty': 10, 'simpleCumQty': None, 'cumQty': 0, 'avgPx': None, 'multiLegReportingType': 'SingleSecurity', 'text': 'Submission from testnet.bitmex.com', 'transactTime': datetime.datetime(2019, 1, 28, 13, 9, 6, 37000, tzinfo=tzutc()), 'timestamp': datetime.datetime(2019, 1, 28, 13, 9, 6, 37000, tzinfo=tzutc())}, {'orderID': 'dfdbd030-87db-ees58f-93c0-76f03b9c9151', 'clOrdID': '', 'clOrdLinkID': '', 'account': 24565, 'symbol': 'XBTUSD', 'side': 'Buy', 'simpleOrderQty': None, 'orderQty': 10, 'price': 2950.0, 'displayQty': None, 'stopPx': None, 'pegOffsetValue': None, 'pegPriceType': '', 'currency': 'USD', 'settlCurrency': 'XBt', 'ordType': 'Limit', 'timeInForce': 'GoodTillCancel', 'execInst': '', 'contingencyType': '', 'exDestination': 'XBME', 'ordStatus': 'New', 'triggered': '', 'workingIndicator': True, 'ordRejReason': '', 'simpleLeavesQty': None, 'leavesQty': 10, 'simpleCumQty': None, 'cumQty': 0, 'avgPx': None, 'multiLegReportingType': 'SingleSecurity', 'text': 'Submission from testnet.bitmex.com', 'transactTime': datetime.datetime(2019, 1, 28, 12, 25, 46, 138000, tzinfo=tzutc()), 'timestamp': datetime.datetime(2019, 1, 28, 12, 25, 46, 138000, tzinfo=tzutc())}, {'orderID': '6bbb1820-efawhe0-a6ec-29d8-308297002eeb', 'clOrdID': '', 'clOrdLinkID': '', 'account': 24565,'symbol': 'XBTUSD', 'side': 'Buy', 'simpleOrderQty': None, 'orderQty': 10, 'price': 3000.0, 'displayQty': None, 'stopPx': None, 'pegOffsetValue': None, 'pegPriceType': '', 'currency': 'USD', 'settlCurrency': 'XBt', 'ordType': 'Limit', 'timeInForce': 'GoodTillCancel', 'execInst': '', 'contingencyType': '', 'exDestination': 'XBME', 'ordStatus': 'New', 'triggered': '', 'workingIndicator': True, 'ordRejReason': '', 'simpleLeavesQty': None, 'leavesQty': 10, 'simpleCumQty': None, 'cumQty': 0, 'avgPx': None, 'multiLegReportingType': 'SingleSecurity', 'text': 'Submission from testnet.bitmex.com', 'transactTime': datetime.datetime(2019, 1, 28, 12, 25, 38, 238000, tzinfo=tzutc()), 'timestamp': datetime.datetime(2019, 1, 28, 12, 25, 38, 238000, tzinfo=tzutc())}]

I would like to have 3 different dictonaries(since this list contains 3) so that i can access values in it.

I tried sorting the list with

 sorted_open =sorted(open[0])
 print(sorted_open) 

this results in the ERROR :

Traceback (most recent call last):
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 62, in wrapped
    ret = await coro(*args, **kwargs)
  File "F:\Test files\Discord_bot\Test.py", line 815, in oo
    open_order = ord.open_orders_order('XBTUSD')
  File "F:\Test files\Discord_bot\Test_orders.py", line 98, in open_orders_order
    sorted_open =sorted(open[0])
TypeError: '<' not supported between instances of 'dict' and 'dict'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\bot.py", line 898, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 615, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Abhishek\AppData\Roaming\Python\Python36\site-packages\discord\ext\commands\core.py", line 71, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: '<' not supported between instances of 'dict' and 'dict'

I have tried bunch of other stuffs but cant get to solve it

Blank
  • 25
  • 8

2 Answers2

2

Try this:

result = sorted(open[0], key=lambda x: x['orderID'])

or:

from operator import itemgetter

result = sorted(open[0], key=itemgetter('orderID'))
Mehrdad Pedramfar
  • 9,313
  • 3
  • 31
  • 54
1

You need a sorting function (example with orderID):

sorted_open =sorted(open[0].items(), key=lambda item: item.orderID)
print(sorted_open)
Benoît P
  • 2,684
  • 9
  • 30