A
A
Andrey Romanov2021-10-24 22:23:54
Python
Andrey Romanov, 2021-10-24 22:23:54

What is the correct way to compare two dictionaries?

Good day! I have two dictionaries of this kind

old_dict = {'Anton':'454.44', 'Sergey':'345.65','Petr':'7634.88'}
new_dict = {'Anton':'1123.20', 'Sergey':'85.34','Petr':'677.09'}

Please tell me how to correctly compare these two dictionaries with the condition if the value in new_dict is greater than in old_dict = display a message.
By keys, it means, compare the numbers, if more, then display a message, and so on for each

Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2021-10-24
@karabanov

old_dict = {'Anton':454.44, 'Sergey':345.65,'Petr':7634.88}
new_dict = {'Anton':1123.20, 'Sergey':85.34,'Petr':677.09}

items = {k: new_dict[k] for k in new_dict if k in old_dict and new_dict[k] > old_dict[k]}
print(items)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question