There are two dictionaries, you need to compare the key value in both dictionaries and write the coincidence in the new dictionary.

operator_one = { 1: 'pass', 2: 'break', 3: 'print', 4: 'yield', 5 :'try' , } operator_two = { 1: 'pass', 2:'global', 3: 'yield', 4: 'print', 5: 'try', } def operator_important(operator_one, operator_two): """Поиск одинаковых элементов в словаре и словаре и запись в словарь.""" operator_main = {} for key, values in operator_one.items: if key, values in operator_two.items: operator_main[key] = values return operator_main d = operator_important(operator_one, operator_two)print(d) 
  • Need to find those whose key and value match? Add the expected result to the question - gil9red
  • Yes, which match the key and the value and write it all into a new dictionary, through a for loop. - Denis Latypov

2 answers 2

Collect the keys of both dictionaries and sort through them:

 operator_one = { 1: 'pass', 2: 'break', 3: 'print', 4: 'yield', 5: 'try', } operator_two = { 1: 'pass', 2: 'global', 3: 'yield', 4: 'print', 5: 'try', } def operator_important(operator_one, operator_two): """Поиск одинаковых элементов в словаре и словаре и запись в словарь.""" operator_main = dict() all_keys = set(list(operator_one.keys()) + list(operator_two.keys())) for key in all_keys: # Если ключ есть в обоих словарях, и значение по ключу одинаковое if key in operator_one and key in operator_two and operator_one[key] == operator_two[key]: operator_main[key] = operator_one[key] return operator_main d = operator_important(operator_one, operator_two) print(d) # {1: 'pass', 5: 'try'} 

Andrey rightly noted that it is possible to simplify the code, like this:

 def operator_important(operator_one, operator_two): """Поиск одинаковых элементов в словаре и словаре и запись в словарь.""" operator_main = dict() for key, value in operator_one.items(): if key in operator_two and value == operator_two[key]: operator_main[key] = value return operator_main 
  • Thanks a great solution i. All through the keys Solution here? In the list I see the conversion list - Denis Latypov
  • The conversion to the list was necessary because of a TypeError: unsupported operand type(s) for +: 'dict_keys' and 'dict_keys' error TypeError: unsupported operand type(s) for +: 'dict_keys' and 'dict_keys' with all_keys = set(operator_one.keys() + operator_two.keys()) - gil9red
  • But why combine the keys from two dictionaries when it is enough to walk through the keys of one, and if this key is not in the second, then their values ​​cannot be equal, due to the absence of the second value for comparison? - Andrey
  • @Andrey, because :) but seriously, there are a lot of solutions to this problem, and combining keys is the first thing that came to mind when I looked at the code in question. And so, of course, it was possible to do as you suggested - gil9red
  • If I do the same thing through * args, should I ask a separate question? - Denis Latypov

Through the generator it will be more compact.

 operator_one = { 2: 'break', 1: 'pass', 3: 'print', 4: 'yield', 5 :'try' , } operator_two = { 1: 'pass', 2:'global', 3: 'yield', 4: 'print', 5: 'try', } d = {key: operator_two[key] for key in operator_one if operator_one[key] == operator_two.get(key)} print(d) # {1: 'pass', 5: 'try'} 

In fact, we thus iterate over all the keys from the first dictionary and compare them with the value of the same key in the second dictionary obtained using the get method, which, if there is no key in the second dictionary, does not throw an exception, but returns the value None ( or any one defined in operator_two.get(key, <some_value>) . Well, the generator itself actually works as it should, adding key-value pairs matching the condition to the new dictionary

  • one
    Or so d = {key: value for key, value in operator_one.items() if key in operator_two and value == operator_two[key]} if you make an honest key check in the second dictionary :) - gil9red
  • Thanks for the options, such a question I do not see operator_main in the generator ?? new dictionary, where does it write? I see only comparison and enumeration, and compact view - Denis Latypov
  • @ DenisLatypov, in this case, a new dictionary is created d If you have a name in principle, you can d = replace with operator_main = - Andrey
  • Grateful now saw. - Denis Latypov