Open In App

Python program to check whether the values of a dictionary are in same order as in a list

Given a dictionary, test if values are in order with list values.

Input : test_dict = {“gfg” : 4, “is” : 10, “best” : 11}, sub_list = [4, 10, 11] 
Output : True 
Explanation : Values are 4, 10, 11, same as list order. Hence True is returned.
 



Input : test_dict = {“gfg” : 4, “is” : 10, “best” : 11}, sub_list = [4, 11, 10] 
Output : False 
Explanation : Values are 4, 10, 11, list order is 4, 11, 10, not same order. Hence False is returned. 

Method #1 : Using loop



In this, we iterate for dictionary values and list alongside, and test if all the values are in order, flag off in case any element is out of order.

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Test for Ordered values from List
# Using loop
 
# initializing dictionary
test_dict = {"gfg" : 4, "is" : 10, "best" : 11, "for" : 19, "geeks" : 1}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
sub_list = [4, 10, 11, 19, 1]
 
idx = 0
res = True
for key in test_dict:
     
    # checking for inequality in order
    if test_dict[key] != sub_list[idx]:
        res = False
        break
    idx += 1
     
# printing result
print("Are values in order : " + str(res))

Output
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'geeks': 1}
Are values in order : True

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

Method #2 : Using values() + comparison operators

In this, we extract all the values using values() and then use comparison operators to check for equality with list.




# Python3 code to demonstrate working of
# Test for Ordered values from List
# Using values() + comparison operators
 
# initializing dictionary
test_dict = {"gfg" : 4, "is" : 10, "best" : 11, "for" : 19, "geeks" : 1}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
sub_list = [4, 10, 11, 19, 1]
 
# comparing values with list
res = list(test_dict.values()) == sub_list
     
# printing result
print("Are values in order : " + str(res))

Output
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'geeks': 1}
Are values in order : True

Method #3: Using sorted() function

  1. Convert the dictionary values to a list using the values() method.
  2. Sort the list using the sorted() function.
  3. Compare the sorted list with the given sub_list.
  4. If both the lists are the same, the dictionary values are in order, else they are not




# initializing dictionary
#test_dict = {"gfg" : 4, "is" : 10, "best" : 11, "for" : 19, "geeks" : 1}
test_dict = {"gfg" : 4, "is" : 10, "best" : 11}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
sub_list = [4, 11, 10]
 
# converting dictionary values to list and sorting
dict_values = list(test_dict.values())
sorted_dict_values = sorted(dict_values)
 
# comparing sorted list with sub_list
res = sorted_dict_values == sub_list
     
# printing result
print("Are values in order : " + str(res))

Output
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11}
Are values in order : False

Time complexity: O(nlogn) due to the use of the sorted() function
Auxiliary space: O(n) for storing the list of dictionary values and sorted list of values

Method #4: Using enumerate() and all() functions

Step-by-step approach:

Below is the implementation of the above approach:




# Initializing dictionary
test_dict = {"gfg" : 4, "is" : 10, "best" : 11, "for" : 19, "geeks" : 1}
 
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Initializing list
sub_list = [4, 10, 11, 19, 1]
 
# Using enumerate() and all() functions to check if values are in order
res = all(sub_list[i] == v for i, v in enumerate(test_dict.values()))
 
# Printing result
print("Are values in order : " + str(res))

Output
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'geeks': 1}
Are values in order : True

Time complexity: O(n), where n is the length of the list or the number of values in the dictionary.
Auxiliary space: O(1), as we only need a constant amount of space to store the variables used in the function.

Method #6: Using map() and list() functions

We can use the map() function to create a new list of dictionary values and then convert it into a list. After that, we can compare this list with the given sub_list using the == operator.

Steps:

  1. Initialize the dictionary and the sub_list.
  2. Use the map() function to create a new list of dictionary values.
  3. Convert the map object into a list.
  4. Compare the new list with the sub_list using the == operator.
  5. Print the result.

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Test for Ordered values from List
# Using map() and list() functions
 
# initializing dictionary
test_dict = {"gfg": 4, "is": 10, "best": 11, "for": 19, "geeks": 1}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
sub_list = [4, 10, 11, 19, 1]
 
# using map() function to create a
# new list of dictionary values
new_list = list(map(lambda x: test_dict[x], test_dict))
 
# comparing new list with sub_list
# using == operator
res = new_list == sub_list
 
# printing result
print("Are values in order : " + str(res))

Output
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'geeks': 1}
Are values in order : True

Time Complexity: O(N), where N is the size of the dictionary.
Auxiliary Space: O(N), where N is the size of the dictionary.


Article Tags :