I am working with a list of dictionaries and I am trying to verify which keys from those dictionaries contain empty values. The data structure that I have is like this:
filtered_data =
[{'id': 1021972, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.94', 'Aging_Un_investigated_Issue': '0.94', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 'Open_date':'2017-08-04 01:34:18', 'End_date': '2017-09-05 00:29:01', 'Ticket_status':'Transferred'},
{'id': 1036722, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.12', 'Aging_Un_investigated_Issue': '0.01', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 'Open_date':'2017-09-01 00:34:18', 'End_date': '', 'Ticket_status':'Researching'},
{'id': 1015621, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.99', 'Aging_Un_investigated_Issue': '0.11', 'User': 'John D.', 'ModifiedOn': '2017-06-05 12:19:59', 'Open_date':'2017-01-01 00:00:18', 'End_date': '2017-09-01 20:20:57', 'Ticket_status':'Closed'}]
The mandatory keys that I want to verify if they contain data are the following:
mandatory_keys = ['id','User','Ticket_status']
And currently I have this code that reads each dictionary from the list and appends only those where the Open_date is contained between the beginning_date_format
and ending_date_format
.
list_data = []
for d in filtered_data:
list_data.append({k: d[k] for k in mandatory_keys if beginning_date_format
<= dateutil.parser.parse(d.get('Open_date'))
< ending_date_format})
I want to add those dictionaries where the Open_date
is contained between beginning and ending dates and where any of the keys from mandatory_keys contain no values " "
.
How can I implement my solution? Any thoughts, comments and suggestions are welcome.
EDIT:
beginning_date_format = '2017-08-01 00:00:00'
ending_date_format = '2017-09-05 00:29:01'
EDIT:
This is the solution I found.
list_data = []
for d in filtered_data:
list_data.append({k: d[k] for k in mandatory_keys if beginning_date_format
<= dateutil.parser.parse(d.get('Open_date'))
< ending_date_format and (d.get('id')==''
or d.get('User')=='' or d.get('Ticket_status')=='')})