I would like to iterate over a dictionary contained in a list and remove the ARRIVAL DATE TIME
items that are older than a certain date.
I've reviewed similar previous questions (similar question, another one, and another one) but given the complexity of my data structure I'm struggling to apply the solutions suggested.
This is what I attempted so far. The solution seems I need to iterate on a duplicate of my data structure, but how do I do it?
from datetime import datetime
ferry = [ {
"NAME":"SNAV Atlair",
"LAST CALLING PORT":"Casamicciola Terme",
"CALLING PORTS HISTORY":{
"MONDAY":[
{
"PORT":"Casamicciola Terme",
"ARRIVAL DATE TIME":"2019-03-19 20:12:10"
},
{
"PORT":"Casamicciola Terme",
"ARRIVAL DATE TIME":"2019-03-19 16:12:10"
}
] ,
"TUESDAY":[
{
"PORT":"Casamicciola Terme",
"ARRIVAL DATE TIME":"2019-03-19 20:12:10"
}
],
"WEDNESDAY":[
],
"THURSDAY":[
],
"FRIDAY":[
],
"SATURDAY":[
],
"SUNDAY":[
]
}
}]
datetime_last_position = '2019-03-23 12:50:00'
for entry in list(ferry):
for i in range(len(entry['CALLING PORTS HISTORY']['MONDAY'])):
if datetime.strptime(entry['CALLING PORTS HISTORY']['MONDAY'][i]['ARRIVAL DATE TIME'], '%Y-%m-%d %H:%M:%S').date() < datetime.strptime(datetime_last_position, '%Y-%m-%d %H:%M:%S').date():
del entry['CALLING PORTS HISTORY']['MONDAY'][i]