I have a dictionary with values being a list of lists, e.g.:
Review_dict["0002"]
[['0002', '0292', '4', datetime.datetime(1998, 2, 27, 11, 1, 34)],
['0002', '0251', '5', datetime.datetime(1998, 2, 27, 12, 2, 24)],
['0002', '0050', '5', datetime.datetime(1998, 2, 27, 12, 3, 24)],
['0002', '0314', '1', datetime.datetime(1998, 3, 4, 10, 5, 45)]]
I want to remove certain list (within the "big" list), where 2nd element = '0251' or '0314'.
e.g. Review_dict["0002"] should become:
[['0002', '0292', '4', datetime.datetime(1998, 2, 27, 11, 1, 34)],
['0002', '0050', '5', datetime.datetime(1998, 2, 27, 12, 3, 24)]]
I have come up with this code but I want to know if there's a simpler/ more elegant way to do so.
remove = ['0251', '0314']
for key in list(Review_dict):
for ID in remove:
for num in range(len(Review_dict[key])):
if Review_dict[key][num][1] == ID:
del Review_dict[key][num]
question from:
https://stackoverflow.com/questions/66060741/filtering-dictionary-with-list-of-lists-as-value