Creating a new list from list of dictionaries - python-3.x

Background
The following code works for adding 2 days to a date (e.g. 1/1/2000 becomes 1/3/2000 and is taken from Altering date in list of dictionary
import datetime
list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/1/2000'},
{'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'},
{'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '5/1/2000'}]
for i in list_of_dic: #Iterate list
if i["type"] == 'DATE': #Check 'type'
i["text"] = (datetime.datetime.strptime(i["text"], "%m/%d/%Y") + datetime.timedelta(days=2)).strftime("%m/%d/%Y") #Increment days.
print(list_of_dic)
Output
[{'id': 'T1', 'type': 'LOCATION-OTHER', 'start': 142, 'end': 148, 'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '01/03/2000'},
{'id': 'T10', 'type': 'DOCTOR', 'start': 692, 'end': 701, 'text': 'Joe'},
{'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '05/03/2000'}]
Question
How would the code change if one wanted to save the output from print(list_of_dic) in a new list called new_list_of_dic?

Just use copy?
new_list_of_dic = list_of_dic.copy()
Or if you want the string?
new_list_of_dic = str(list_of_dic)

Related

How to add "data" to an array?

I have the below metadata in an variable, but how can I add it to an array? I would like add the "data" to an array.
{'count': 130447,
'data': [{'amount': '5',
'date': 1653943413806,
'direction': 'BUY',
'id': 314901,
'matched_order_id': 108327933,
'order_id': 108336049,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48},
{'amount': '40',
'date': 1653943403928,
'direction': 'BUY',
'id': 314900,
'matched_order_id': 108327933,
'order_id': 108336031,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48}],
'limit': 2,
'offset': 0}
Im not sure if I really get you question, please make it clearer, but from what I understood, do you want this ?
import numpy as np
dictionary = {'count': 130447,
'data': [{'amount': '5',
'date': 1653943413806,
'direction': 'BUY',
'id': 314901,
'matched_order_id': 108327933,
'order_id': 108336049,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48},
{'amount': '40',
'date': 1653943403928,
'direction': 'BUY',
'id': 314900,
'matched_order_id': 108327933,
'order_id': 108336031,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48}],
'limit': 2,
'offset': 0}
arr = np.array(dictionary['data'])
print(arr)
output:
[{'amount': '5', 'date': 1653943413806, 'direction': 'BUY', 'id': 314901, 'matched_order_id': 108327933, 'order_id': 108336049, 'price': '0.1326', 'symbol': 'ECS/EUR', 'trading_pair_id': 48}
{'amount': '40', 'date': 1653943403928, 'direction': 'BUY', 'id': 314900, 'matched_order_id': 108327933, 'order_id': 108336031, 'price': '0.1326', 'symbol': 'ECS/EUR', 'trading_pair_id': 48}]

How to append more data to a dictionary

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.
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]}
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

How to collect a value from a 3 key in a dictionary?

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}
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
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'])

Looping through list of dictionary

Background
I have a list a dictionaries as seen below:
list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/1/2000'},
{'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'},
{'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '5/1/2000'}]
Goal
Use an if statement or for statement to print everything except for 'type': 'DATE
Example
I would like for it to look something like this:
for dic in list_of_dic:
#skip 'DATE' and corresponding 'text'
if edit["type"] == 'DATE':
edit["text"] = skip this
else:
print everything else that is not 'type':'DATE' and corresponding 'text': '1/1/2000'
Desired Output
list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'}]
Question
How do I achieve my desired output using loops?
Try this:
list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/1/2000'},
{'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'},
{'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '5/1/2000'}]
newList = []
for dictionary in list_of_dic:
if dictionary['type'] != 'DATE':
newList.append(dictionary)
Use list comprehension:
In [1]: list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text'
...: : 'California'},
...: {'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
...: {'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/1/2000'},
...: {'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'},
...: {'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '5/1/2000'}]
In [2]: out = [i for i in list_of_dic if i['type'] != 'DATE']
In [3]: out
Out[3]:
[{'id': 'T1',
'type': 'LOCATION-OTHER',
'start': 142,
'end': 148,
'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T10', 'type': 'DOCTOR', 'start': 692, 'end': 701, 'text': 'Joe'}]

Altering date in list of dictionary

Background
I have a list of dictionary values
list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/1/2000'},
{'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'},
{'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '5/1/2000'}]
Goal
1) Locate all 'type': 'DATE' in list_of_dict
2) Add 2 days to the corresponding 'text: 'value'
Example
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/1/2000'}
will become
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/3/2000'}
Desired Output
desired_list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/3/2000'},
{'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'},
{'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '5/3/2000'}]
Use datetime module
Ex:
import datetime
list_of_dic = [{'id': 'T1','type': 'LOCATION-OTHER','start': 142,'end': 148,'text': 'California'},
{'id': 'T2', 'type': 'PHONE', 'start': 342, 'end': 352, 'text': '123456789'},
{'id': 'T3', 'type': 'DATE', 'start': 679, 'end': 687, 'text': '1/1/2000'},
{'id': 'T10','type': 'DOCTOR','start': 692,'end': 701,'text': 'Joe'},
{'id': 'T11', 'type': 'DATE', 'start': 702, 'end': 710, 'text': '5/1/2000'}]
for i in list_of_dic: #Iterate list
if i["type"] == 'DATE': #Check 'type'
i["text"] = (datetime.datetime.strptime(i["text"], "%m/%d/%Y") + datetime.timedelta(days=2)).strftime("%m/%d/%Y") #Increment days.
print(list_of_dic)
Output:
[{'end': 148,
'id': 'T1',
'start': 142,
'text': 'California',
'type': 'LOCATION-OTHER'},
{'end': 352, 'id': 'T2', 'start': 342, 'text': '123456789', 'type': 'PHONE'},
{'end': 687, 'id': 'T3', 'start': 679, 'text': '01/03/2000', 'type': 'DATE'},
{'end': 701, 'id': 'T10', 'start': 692, 'text': 'Joe', 'type': 'DOCTOR'},
{'end': 710, 'id': 'T11', 'start': 702, 'text': '05/03/2000', 'type': 'DATE'}]
datetime.timedelta(days=N) --> to increment days
datetime.datetime.strptime --> to convert string to datetime object
strftime --> to convert datetime object to string

Resources