Python | Difference in keys of two dictionaries
Given two dictionaries dic1 and dic2 which may contain same-keys, find the difference of keys in given dictionaries.
Code #1 : Using set to find keys that are missing.
# Python code to find the difference in # keys in two dictionary # Initialising dictionary dict1= {'key1':'Geeks', 'key2':'For', 'key3':'geeks'} dict2= {'key1':'Geeks', 'key2:':'Portal'} diff = set(dict2) - set(dict1) # Printing difference in # keys in two dictionary print(diff) |
chevron_right
filter_none
Output:
{'key2:'}
Code #2 : Finding keys in dict2 which are not in dict1.
# Python code to find difference in keys in two dictionary # Initialising dictionary dict1= {'key1':'Geeks', 'key2':'For'} dict2= {'key1':'Geeks', 'key2':'For', 'key3':'geeks', 'key4': {'GeekKey1': 12, 'GeekKey2': 22, 'GeekKey3': 32 }} for key in dict2.keys(): if not key in dict1: # Printing difference in # keys in two dictionary print(key) |
chevron_right
filter_none
Output:
key4 key3
Code #3: Finding keys in dict1 which are not in dict2.
# Python code to find difference in keys in two dictionary # Initialising dictionary dict1= {'key1':'Geeks', 'key12':'For'} dict2= {'key1':'Geeks', 'key2':'For', 'key3':'geeks', 'key4': {'GeekKey1': 12, 'GeekKey2': 22, 'GeekKey3': 32 }} for key in dict1.keys(): if not key in dict2: # Printing difference in # keys in two dictionary print(key) |
chevron_right
filter_none
Output:
key12
Code #4 : Finding the same keys in two dictionaries.
# Python code to find difference in keys in two dictionary # Initialising dictionary dict1= {'key1':'Geeks', 'key2':'For'} dict2= {'key1':'Geeks', 'key2':'For', 'key3':'geeks', 'key4': {'GeekKey1': 12, 'GeekKey2': 22, 'GeekKey3': 32 }} print(set(dict1.keys()).intersection(dict2.keys())) |
chevron_right
filter_none
Output:
{'key2', 'key1'}
Recommended Posts:
- Python | Intersect two dictionaries through keys
- Handling missing keys in Python dictionaries
- Python | Set Difference in list of dictionaries
- Python | Sum list of dictionaries with same key
- Python | Merging two Dictionaries
- Get() method for dictionaries in Python
- Python | Combine the values of two dictionaries having same key
- Python | Flatten given list of dictionaries
- Python | Merging two list of dictionaries
- Python | Sort given list of dictionaries by date
- Python | Removing dictionary from list of dictionaries
- Python | Split dictionary of lists to list of dictionaries
- Python | Remove duplicate dictionaries from nested dictionary
- Python | Sort Python Dictionaries by Key or Value
- Ways to sort list of dictionaries by values in Python – Using itemgetter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



