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:
- The program first initializes a dictionary named test_dict with some key-value pairs.
- Then it prints the original dictionary using print statement.
- It initializes a list named sub_list with some values in a particular order.
- It initializes two variables idx and res to 0 and True respectively.
- It then enters a loop over each key in the dictionary using for loop.
- Within the loop, it checks whether the value of the current key in the dictionary matches the value at the corresponding index in the sub_list.
- If the values are not equal, it sets the value of res to False and exits the loop using break.
- If the loop completes without any inequality, it means the values are in order and res remains True.
- Finally, it prints the result of whether the values are in order using print statement along with the value of res.
Below is the implementation of the above approach:
Python3
# 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 , "neveropen" : 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)) |
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'neveropen': 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
# 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 , "neveropen" : 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)) |
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'neveropen': 1} Are values in order : True
Method #3: Using sorted() function
- Convert the dictionary values to a list using the values() method.
- Sort the list using the sorted() function.
- Compare the sorted list with the given sub_list.
- If both the lists are the same, the dictionary values are in order, else they are not
Python3
# initializing dictionary #test_dict = {"gfg" : 4, "is" : 10, "best" : 11, "for" : 19, "neveropen" : 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)) |
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:
- Initialize the dictionary and the list as given in the problem statement.
- Use the enumerate() function to iterate over the dictionary’s values and compare them to the corresponding values in the list. If a value is found that does not match the list’s corresponding value, return False.
- Print the result.
Below is the implementation of the above approach:
Python3
# Initializing dictionary test_dict = { "gfg" : 4 , "is" : 10 , "best" : 11 , "for" : 19 , "neveropen" : 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)) |
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'neveropen': 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:
- Initialize the dictionary and the sub_list.
- Use the map() function to create a new list of dictionary values.
- Convert the map object into a list.
- Compare the new list with the sub_list using the == operator.
- Print the result.
Below is the implementation of the above approach:
Python3
# 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 , "neveropen" : 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)) |
The original dictionary is : {'gfg': 4, 'is': 10, 'best': 11, 'for': 19, 'neveropen': 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.