Im trying to remove a element from a dict only if the condition is met.
For example,
dd = {'11': [{'xx': 259, 'yy': 1, 'channels': 55}, {'xx': 260, 'yy': 2, 'channels': 35}], '22': [{'xx': 259, 'yy': 1, 'channels': 40}, {'xx': 303, 'yy': 2, 'channels': 30}]}
Scenario:
channels = 60
there are two elements with key xx = 259 in the nested dict above, I wish to keep only one element which is closer to the channels = 60 value.
expected output:
dd = {'11': [{'xx': 259, 'yy': 1, 'channels': 55}, {'xx': 260, 'yy': 2, 'channels': 35}], '22': [{'xx': 303, 'yy': 2, 'channels': 30}]}
I tried so far:
channels = 60
for key, values in dd.items():
key = str(key)
if key in dd:
for elem in dd[key]:
if elem['xx'] == 259:
print(elem)
# some logic here to check the closest value to channels and remove the furthest one
Which outputs:
{'xx': 259, 'yy': 1, 'channels': 55}
{'xx': 259, 'yy': 1, 'channels': 40}
UPDATED APPROACH:
channels = 60
xList = []
for key, values in dd.items():
key = str(key)
if key in dd:
for elem in dd[key]:
if elem['xx'] == 259:
xList.append(elem['channels'])
result =min(xList, key=lambda x: abs(x - 60))
# find and remove elem from dict
for key, values in dd.items():
key = str(key)
if key in dd:
for elem in dd[key]:
if elem['xx'] == 259:
if elem['channels'] == result:
pass
else:
print("delete this elem: ", elem)
dd[key].remove(elem)
print(dd)
OUTPUT:
{'11': [{'xx': 259, 'yy': 1, 'channels': 55}, {'xx': 260, 'yy': 2, 'channels': 35}], '22': [{'xx': 303, 'yy': 2, 'channels': 30}]}
I managed to achieve the end goal, but I feel like this can certainly be improved. Any help?