I have the following example, I have seen the execution flow of the piece of code, and I know it has a problem thus I cannot get the desired result from the code. However, I have tried many ways and still do not know, how can I get the desired result.
Code:
dict1 = {10: [22], 40: [50, 20, 60]}
new_dict = {}
for k, v in dict1.items():
for each_v in v:
if each_v < k:
v.remove(each_v)
# temp_v = v.copy()
# temp_v.remove(each_v)
new_dict[each_v] = [k] + v
# print(temp_v)
# print(v)
else:
new_dict[k] = v
print(new_dict)
Current output:
{10: [22], 20: [40, 50, 60], 40: [50, 60]}
Desired output:
{10: [22], 20: [40, 50, 60]}
*keep the original order*
What I only want, is to exchange the key and one of its values IF the key is bigger than this value. I do not want to add more item to the dictionary. Also
If any of you has a hint, please do comment, thanks in advance!