Assume I have this:
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
and I want to add this to the dictionary:
{'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
So it will look like this: (If it possible.)
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373},
{'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
I'm not good to python, so I don't know if this is possible. If it is possible how do I search for element: 'trading_pair_id': 50 and if the 'id' for that index has changed I want to update all elements with new data for that index.
CodePudding user response:
I dont think the output you mentioned in your question is a valid dictionary.. It's more like a tuple.. So I came up with my code to join these two dictionaries.
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
another_tradedict = {'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
for i in tradedict:
temp = []
temp.append(tradedict[i])
temp.append(another_tradedict[i])
tradedict[i] = temp
print(tradedict)
And the output would be something like this,
{'id': [325920, 325910], 'order_id': [109185014, 109179557], 'matched_order_id': [109181538, 109179004], 'direction': ['BUY', 'BUY'], 'trading_pair_id': [48, 50], 'symbol': ['ECS/EUR', 'BTC/ECS'], 'amount': ['1', '0.001'], 'price': ['0.1507', '193499.99'], 'date': [1654433352373, 1654429749384]}
CodePudding user response:
You cannot repeat keys in a dictionary
What I meant to say is,
Your primary dictionary is having a key name "id", "order_id" etc
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
But the dictionary you wish to add also has the same keys, hence you will not be able to add these two dictionaries in particular.
But what you can do is:
You can add new key-value pairs in your dictionary
tradeict["Company"]="amazon"
You will get this as a result
{'id': 325920,
'order_id': 109185014,
'matched_order_id': 109181538,
'direction': 'BUY',
'trading_pair_id': 48,
'symbol': 'ECS/EUR',
'amount': '1',
'price': '0.1507',
'date': 1654433352373,
'company': 'amazon'}
Notice how new key value pair has been added to the dictionary