Python dictionnaries have keys and values accessed using those keys.

You can access the keys as follows, your dict key will be stored in the key variable:

my_dict = {"a" : 1, "b" : 2}
for key in my_dict:
    print(key)

This will print:

a
b

You can then do any comparisons you want:

my_dict = {"a" : 1, "b" : 2}
for key in my_dict:
    if key == "a":
        return True
    else:
        return False

which can be improved to:

my_dict = {"a" : 1, "b" : 2}
print("a" in my_dict.keys())

You can then access the values for each key in your dict as follows:

my_dict = {"a" : 1, "b" : 2}
for key in my_dict:
    print(my_dict[key])

This will print:

1
2

I suggest you read more about dictionaries from the official Python documentation: https://docs.python.org/3.6/tutorial/datastructures.html#dictionaries

Answer from Adam J on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-do-I-compare-two-different-dictionary-values-in-Python
How to compare two different dictionary values in Python - Quora
If you just want to compare the actual values: just use the `cmp` method in python 2 or if your using python3: How can I use cmp(a,b) with Python3?. Or just write your own. In python, a dictionary is a key -> value pair, so for each key that ...
Discussions

python - how to compare a key of the dictionary with a string character - Stack Overflow
I am new to python and I need to compare a character in string with a key in a dictionary. But I am not able to figure out a way to compare that character with a key. I am only able to compare it w... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I compare a string with a dictionary element? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... how can I check if a string is inside a dictionary? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Comparing Python dictionary values if one dictionary has values equal, or more than the another
There is no way to compare dictionaries directly in Python3 and I think it is for the best. What should the comparison operators compare anyway ? Keys ? Values ? Their lengths ? Your dictionaries are pairs of items and its count, so it could make sense to compare them, but dictionaries generally can store anything, and I wonder how would the comparison work if we stored for example pairs of countries - their capital, what would be compared then. Even in your case, there might be some discussion, for example if A had bigger one count in one dict and B had bigger counter in second dict: first = {A : 3, B : 1} second = {A : 1, B : 2} #which one is bigger Would that mean that neither of them is bigger, since both have some value smaller then the other one ? Comparison operators usually provide a way to check which one is bigger, which would not be the case here. Long story short, it will be the best for you to write a function that compares 2 dictionaries the way you want it to and use that. Even the built-in collections.Counter() does not support comparison operators from reasons described above. More on reddit.com
๐ŸŒ r/learnpython
11
1
March 30, 2021
python - Comparing two dictionaries and checking how many (key, value) pairs are equal - Stack Overflow
I have two dictionaries, but for simplification, I will take these two: >>> x = dict(a=1, b=2) >>> y = dict(a=2, b=2) Now, I want to compare whether each key, value pair in x has... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Codesolid
codesolid.com โ€บ how-to-compare-python-dictionaries
How To Compare Python Dictionaries โ€” CodeSolid.com 0.1 documentation
The easiest way to compare Python dictionaries is simply to use the same equality operator you use on other Python types, โ€œ==โ€. This works great for most cases, but it relies on the behavior of โ€œ==โ€ for the values involved. If you need a more strict test, this can be done in a few lines ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-compare-two-dictionaries-in-python
How to Compare Two Dictionaries in Python - GeeksforGeeks
April 25, 2025 - Two dictionaries, d1 and d2, are defined with the same key-value pairs. The if statement checks if the lengths of the dictionaries are different. If they are, it prints False. A for loop iterates through each key in d1 to check. If the key is missing in d2 or if the corresponding values differ, it prints False and stops the loop. If no mismatches are found, the else block prints True, indicating the dictionaries are equal. ... In Python, dictionaries are unordered collections of key-value pairs.
๐ŸŒ
Miguendes
miguendes.me โ€บ the-best-way-to-compare-two-dictionaries-in-python
The Best Way to Compare Two Dictionaries in Python
April 29, 2024 - We'll adopt a library called deepdiff, from zepworks. deepdiff can pick up the difference between dictionaries, iterables, strings and other objects. It accomplishes that by searching for changes in a recursively manner. deepdiff is not the ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-compare-dictionaries-on-certain-keys
Python - Compare Dictionaries on certain Keys - GeeksforGeeks
April 21, 2023 - In this, we iterate for both the dictionary and manually test for keys equality using equality operator from selected keys. ... # Python3 code to demonstrate working of # Compare Dictionaries on certain Keys # Using loop # initializing dictionaries test_dict1 = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'geeks' : 5} test_dict2 = {'gfg' : 2, 'is' : 3, 'best' : 3, 'for' : 7, 'geeks' : 5} # printing original dictionaries print("The original dictionary 1 : " + str(test_dict1)) print("The original dictionary 2 : " + str(test_dict2)) # initializing compare keys comp_keys = ['best', 'geeks'] # Compare Dictionaries on certain Keys # Using loop res = True for key in comp_keys: if test_dict1.get(key) != test_dict2.get(key): res = False break # printing result print("Are dictionary equal : " + str(res))
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python program to compare dictionary
Python Program to Compare Dictionary- Scaler Topics
April 9, 2024 - In this article, we learned various ways in which we can do Python dictionary compare. The method we discussed: Using == operator: The equality operator can be used to compare two dictionaries. This is the most straightforward method.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-program-to-compare-elements-in-two-dictionaries
Python Program to compare elements in two dictionaries
February 17, 2023 - We came across 3 different methods to compare 2 dictionaries. The first method involved use of equality operator ( ==). The second method involved use of iteration to check each and every key value pair of both the dictionaries. In the final method we used the list comprehension method of python ...
๐ŸŒ
SitePoint
sitepoint.com โ€บ python hub โ€บ dictionary comparison
Python - Dictionary Comparison | SitePoint โ€” SitePoint
This approach will not work for nested dictionaries, as in Python dictionaries are mutable and, therefore, cannot be elements of a set. To correctly compare nested dictionaries, you need to use recursion or special libraries such as deepdiff.
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to compare two dictionaries in python
How to compare two dictionaries in Python | Replit
February 12, 2026 - You can efficiently track updates to user data, like profile information, by comparing old and new dictionary states to isolate what has changed with AI coding. old_profile = {'name': 'John', 'bio': 'Python developer', 'followers': 120} new_profile = {'name': 'John', 'bio': 'Python & JS developer', 'followers': 145} keys_to_check = set(old_profile) | set(new_profile) changes = {k: (old_profile.get(k), new_profile.get(k)) for k in keys_to_check if old_profile.get(k) != new_profile.get(k)} print(f"Profile changes: {changes}")
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-comparison
Python Compare Strings - Methods & Best Practices | DigitalOcean
April 17, 2025 - This discrepancy occurs because ... Unicode code point value of the characters. In Python, there are three primary methods for comparing strings: ==, is, and cmp()....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ comparing python dictionary values if one dictionary has values equal, or more than the another
r/learnpython on Reddit: Comparing Python dictionary values if one dictionary has values equal, or more than the another
March 30, 2021 -

Here is the scenario:

dict1 = { A : 1 , B : 3 , C: 0 }

dict2 = {C : 1, D : 2 , A : 1, B : 3 }

dict3 = {E : 1, B : 3, A : 1, C : 0}

Assuming dict1 is the baseline that subsequent dictionaries (dict2, dict3) need to be compared against, I am thinking about codes that check if dict 2 >= dict1 or dict 3 >= dict1:

  1. I understand dictionary is unordered, so does that mean that the dictionary can be compared without sort method?

  2. does extra elements in dict2 and dict3 affect the comparison operation, should my code factor extra elements?

  3. i think the best way would be something similar to cmp() in Python2, is any function similar to cmp() for dictionary? I don't intend to import any outside library, but okay with builtin modules.

I would like to some pointers to solve the question. Thank you for the help in advance!

Top answer
1 of 3
2
There is no way to compare dictionaries directly in Python3 and I think it is for the best. What should the comparison operators compare anyway ? Keys ? Values ? Their lengths ? Your dictionaries are pairs of items and its count, so it could make sense to compare them, but dictionaries generally can store anything, and I wonder how would the comparison work if we stored for example pairs of countries - their capital, what would be compared then. Even in your case, there might be some discussion, for example if A had bigger one count in one dict and B had bigger counter in second dict: first = {A : 3, B : 1} second = {A : 1, B : 2} #which one is bigger Would that mean that neither of them is bigger, since both have some value smaller then the other one ? Comparison operators usually provide a way to check which one is bigger, which would not be the case here. Long story short, it will be the best for you to write a function that compares 2 dictionaries the way you want it to and use that. Even the built-in collections.Counter() does not support comparison operators from reasons described above.
2 of 3
1
What exactly do you mean with dict 2 >= dict1? Like a subset? You should use sets for that, not dictionaries. https://docs.python.org/3/library/stdtypes.html#set You could convert your dictionaries to sets like this: set1 = set(dict1.keys()) # if you want to compare keys only set1 = set(dict1.values()) # if you want to compare values only set1 = set(dict1.items()) # if you want to compare both
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ compare two dictionaries and check if key-value pairs are equal
Compare Two Dictionaries and Check if Key-Value Pairs are Equal - AskPython
February 15, 2023 - deepdiff is a python package for deep difference and search of any Python object/data. As per the docs, it is used for Deep Differences in dictionaries, iterable, strings, and other objects.
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-compare-dictionaries-methods-examples
PyTutorial | Python Compare Dictionaries: Methods & Examples
January 27, 2026 - Comparing dictionaries in Python is essential. The == operator handles most simple cases. For more control, use set operations on keys or write custom functions. For nested data, rely on Python's built-in deep equality.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ comparing list values to dictionary keys?
r/learnpython on Reddit: Comparing List Values to Dictionary Keys?
April 30, 2021 -

Hi All,

I am working on something and I need to compare the values of a list to keys in a dictionary to find if they are they same. Then, if they are the same, print the value of that key. What is the best way to go about this? I tried:

alpha = {
'a' : 1 
'b' : 2 
'c' : 3 
}

lst = [ 'a', 'b', 'c'] 

for i in alpha:
    if i == alpha[i]:
        print alpha[i]
    else:
        return False

Every time I ran the code it failed. Can someone explain why this doesn't work and how to fix it?