The Wayback Machine - https://web.archive.org/web/20230316115708/https://www.geeksforgeeks.org/python-test-if-all-values-are-same-in-dictionary/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Test if all Values are Same in Dictionary

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 11 Mar, 2023
Improve Article
Save Article

Given a dictionary, test if all its values are the same.

Input : test_dict = {"Gfg" : 8, "is" : 8, "Best" : 8} 
Output : True 
Explanation : All element values are same, 8. 

Input : test_dict = {"Gfg" : 8, "is" : 8, "Best" : 9} 
Output : False 
Explanation : All element values not same.

Method #1: Using loop

This is one of the ways in which this task can be performed. In this, we iterate for all the values and compare with value in dictionary, if any one is different, then False is returned.

Python3




# Python3 code to demonstrate working of
# Test if all Values are Same in Dictionary
# Using loop
 
# initializing dictionary
test_dict = {"Gfg": 5, "is": 5, "Best": 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Flag to check if all elements are same
res = True
 
# extracting value to compare
test_val = list(test_dict.values())[0]
 
for ele in test_dict:
    if test_dict[ele] != test_val:
        res = False
        break
 
# printing result
print("Are all values similar in dictionary? : " + str(res))

Output

The original dictionary is : {'Gfg': 5, 'is': 5, 'Best': 5}
Are all values similar in dictionary? : True

Time complexity: O(n), where n is the number of key-value pairs in the dictionary. 
Auxiliary space: O(1), as the memory usage does not depend on the size of the input.

Method #2: Using set() + values() + len()

This is yet another way in which this task can be performed. In this, we extract all the values using values() and set() is used to remove duplicates. If length of the extracted set is 1, then all the values are assumed to be similar.

Python3




# Python3 code to demonstrate working of
# Test if all Values are Same in Dictionary
# Using set() + values() + len()
 
# initializing dictionary
test_dict = {"Gfg" : 5, "is" : 5, "Best" : 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using set() to remove duplicates and check for values count
res = len(list(set(list(test_dict.values())))) == 1
 
# printing result
print("Are all values similar in dictionary? : " + str(res))

Output

The original dictionary is : {'Gfg': 5, 'is': 5, 'Best': 5}
Are all values similar in dictionary? : True

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), as we create a list of values from the dictionary.

Method #3: Using values()+len()+count() methods

Python3




# Python3 code to demonstrate working of
# Test if all Values are Same in Dictionary
 
# initializing dictionary
test_dict = {"Gfg": 5, "is": 5, "Best": 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
x = list(test_dict.values())
res = False
if(x.count(x[0]) == len(x)):
    res = True
 
# printing result
print("Are all values similar in dictionary? : " + str(res))

Output

The original dictionary is : {'Gfg': 5, 'is': 5, 'Best': 5}
Are all values similar in dictionary? : True

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.

Method #4: Using values() and len() methods

Python3




# Python3 code to demonstrate working of
# Test if all Values are Same in Dictionary
 
# initializing dictionary
test_dict = {"Gfg": 5, "is": 5, "Best": 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
x = list(test_dict.values())
res = False
if([x[0]]*len(x) == x):
    res = True
# printing result
print("Are all values similar in dictionary? : " + str(res))

Output

The original dictionary is : {'Gfg': 5, 'is': 5, 'Best': 5}
Are all values similar in dictionary? : True

Method #5: Using Counter() function

Python3




# Python3 code to demonstrate working of
# Test if all Values are Same in Dictionary
from collections import Counter
# initializing dictionary
test_dict = {"Gfg": 5, "is": 5, "Best": 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
x = list(test_dict.values())
res = False
freq = Counter(x)
if(len(freq) == 1):
    res = True
# printing result
print("Are all values similar in dictionary? : " + str(res))

Output

The original dictionary is : {'Gfg': 5, 'is': 5, 'Best': 5}
Are all values similar in dictionary? : True

Time Complexity:O(n)
Auxiliary Space:O(n)

Method #6: Using all() function

This approach uses the built-in all() function to check if all values in the dictionary are equal to the first value.

Python3




# Python3 code to demonstrate working of
# Test if all Values are Same in Dictionary
# Using all() function
 
# initializing dictionary
test_dict = {"Gfg": 5, "is": 5, "Best": 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# extracting value to compare
test_val = list(test_dict.values())[0]
 
# checking if all values are equal to test_val
res = all(val == test_val for val in test_dict.values())
 
# printing result
print("Are all values similar in dictionary? : " + str(res))

Output

The original dictionary is : {'Gfg': 5, 'is': 5, 'Best': 5}
Are all values similar in dictionary? : True

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #7 : Using values()+len()+operator.countOf() methods

Approach

  1. Extract the dictionary values using values() method
  2. Initially set res to False
  3. Check whether the count of first element of values list is equal to length of list
  4. If yes set res to True
  5. Display res

Python3




# Python3 code to demonstrate working of
# Test if all Values are Same in Dictionary
 
# initializing dictionary
test_dict = {"Gfg": 5, "is": 5, "Best": 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
x = list(test_dict.values())
res = False
import operator
if(operator.countOf(x,x[0]) == len(x)):
    res = True
 
# printing result
print("Are all values similar in dictionary? : " + str(res))

Output

The original dictionary is : {'Gfg': 5, 'is': 5, 'Best': 5}
Are all values similar in dictionary? : True

Time Complexity : O(N)

Auxiliary Space : O(N)

N- no of iterations took for checking the count of first element in list


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!