Home > Software design >  Deleting Keys (datetime objects) of dict in efficient way
Deleting Keys (datetime objects) of dict in efficient way

Time:09-17

I have a (nested) dict. The keys are datetime objects. Im trying to only keep the latest items, in this case everything older than 60 seconds should be deleted.

Since the application is time critical I am looking for a faster solution. I doubt that my solution is good/fast or think that there are faster solutions.

Can someone please give me a thought on how I could solve it instead to save some steps and time.

Code:

import datetime
import copy

test = {'record1': {datetime.datetime(2021, 9, 14, 1, 3, 3, 781905): 'A', datetime.datetime(2021, 9, 14, 1, 4, 3, 876008): 'B', datetime.datetime(2021, 9, 14, 1, 36, 6, 557661): 'C' }}
dict2 = copy.deepcopy(test)
keys = dict2["record1"].keys()
compare = datetime.datetime.now() - datetime.timedelta(seconds=60)

for key in keys:
    
    if key >= compare:
        print(key)
    else:
        del test['record1'][key]

print(test)

Output:

2021-09-14 01:36:06.557661
{'record1': {datetime.datetime(2021, 9, 14, 1, 36, 6, 557661): 'C'}}

CodePudding user response:

You don't have to make deepcopy of dictionary - you can use dictionary-comprehension with filtering. For example:

import datetime

test = {
    "record1": {
        datetime.datetime(2021, 9, 14, 0, 3, 3, 781905): "A",
        datetime.datetime(2021, 9, 14, 0, 4, 3, 876008): "B",
        datetime.datetime(2021, 9, 14, 0, 7, 6, 557661): "C",
    }
}

compare = datetime.datetime.now() - datetime.timedelta(seconds=60)

test = {
    k: {kk: vv for kk, vv in v.items() if kk > compare} for k, v in test.items()
}
print(test)

Prints:

{'record1': {datetime.datetime(2021, 9, 14, 0, 7, 6, 557661): 'C'}}
  • Related