Open In App

# Python | Test if dictionary contains unique keys and values

Sometimes, we just wish to work with unique elements and any type of repetition is not desired, for these cases, we need to have techniques to solve these problems. One such problem can be to test for unique keys and values. For keys, they are by default unique, hence no external testing is required, but as for values, we need to have ways to do it. Let’s test various ways in which this can be done.

Method #1: Using loops In the Naive method to perform this particular task, we can check for each value and insert each value in list/hash in dictionary and when i’l repetition occurs, just stop the flow and return false.

## Python3

 # Python3 code to demonstrate# check for unique values# Using loops # initializing dictionarytest_dict = {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1} # printing original dictionaryprint("The original dictionary : " + str(test_dict)) # using loops# check for unique valuesflag = Falsehash_val = dict()for keys in test_dict:    if test_dict[keys] in hash_val:        flag = True        break    else:        hash_val[test_dict[keys]] = 1 # print resultprint("Does dictionary contain repetition : " + str(flag))

Output

The original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True

Time Complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary Space: O(n), where n is the number of elements in the dictionary, as we are storing all the values in the hash_val dictionary.

Method #2 :Using len() + set() + values() This problem can be easily solved using the combination of above three functions. The set function can be used to convert the values to set, removing duplicates and values function can be used to access the values.

## Python3

 # Python3 code to demonstrate# check for unique values# Using len() + set() + values() # initializing dictionarytest_dict = {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1} # printing original dictionaryprint("The original dictionary : " + str(test_dict)) # using len() + set() + values()# check for unique valuesflag = len(test_dict) != len(set(test_dict.values())) # print resultprint("Does dictionary contain repetition : " + str(flag))

Output

The original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True

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

Method #3 : Using values() and count() method

## Python3

 # Python3 code to demonstrate# check for unique values # initializing dictionarytest_dict = {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1} # printing original dictionaryprint("The original dictionary : " + str(test_dict)) # using loops# check for unique valuesflag = Falsex = list(test_dict.values())for i in x:    if x.count(i) > 1:        flag = True        break# print resultprint("Does dictionary contain repetition : " + str(flag))

Output

The original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4 : Using Counter() function

## Python3

 # Python3 code to demonstrate# check for unique valuesfrom collections import Counter# initializing dictionarytest_dict = {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}# printing original dictionaryprint("The original dictionary : " + str(test_dict))flag = TruefreqKeys = Counter(list(test_dict.keys()))freqValues = Counter(list(test_dict.values()))if(freqKeys == len(test_dict) and freqValues == len(test_dict)):    flag = False# print resultprint("Does dictionary contain repetition : " + str(flag))

Output

The original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True

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

Method 5:  using operator.countOf() method

## Python3

 # Python3 code to demonstrate# check for unique valuesimport operator as op# initializing dictionarytest_dict = {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1} # printing original dictionaryprint("The original dictionary : " + str(test_dict)) # using loops# check for unique valuesflag = Falsex = list(test_dict.values())for i in x:    if op.countOf(x, i) > 1:        flag = True        break# print resultprint("Does dictionary contain repetition : " + str(flag))

Output

The original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True

Time Complexity: O(N)
Auxiliary Space : O(1)

Method 6: Using set() function

In this method, we create a set of the values of the dictionary using the set() function, which only contains unique elements. We compare the length of this set with the length of the original dictionary. If they are equal, it means that all the values in the dictionary are unique.

## Python3

 # Python3 code to demonstrate# check for unique values # initializing dictionarytest_dict = {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1} # printing original dictionaryprint("The original dictionary : " + str(test_dict)) # check for unique valuesflag = len(test_dict) == len(set(test_dict.values())) # print resultprint("Does dictionary contain repetition : " + str(not flag))

Output

The original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True

The time complexity of the given code is O(n), where n is the number of key-value pairs in the dictionary.
The auxiliary space used by the code is O(n), where n is the number of key-value pairs in the dictionary.

Method 7: Using the lambda function and map():

Algorithm:

1. Initialize a dictionary test_dict with key-value pairs.
2. Using lambda function and map(), extract all the values from the dictionary and convert them into a list.
3. Use set() to extract unique values from the list.
4. Check if the length of the original list and the set are equal or not. If they are not equal, then the dictionary contains repetitions.
5. Print the result.

## Python3

 # check for unique values  # initializing dictionarytest_dict = {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}# printing original dictionaryprint("The original dictionary : " + str(test_dict))flag = len(list(map(lambda x: x, test_dict.values()))) != len(set(test_dict.values()))# print resultprint("Does dictionary contain repetition : " + str(flag)) #This code is contributed by Jyothi Pinjala.

Output

The original dictionary : {'Manjeet': 1, 'Akash': 2, 'Akshat': 3, 'Nikhil': 1}
Does dictionary contain repetition : True

Time complexity: O(n), where n is the number of values in the dictionary.
Space complexity: O(n), where n is the number of values in the dictionary (for the list created using map() function).has context menu