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
test_dict = { "gfg" : 4 , "is" : 10 , "best" : 11 , "for" : 19 , "geeks" : 1 }
print ( "The original dictionary is : " + str (test_dict))
sub_list = [ 4 , 10 , 11 , 19 , 1 ]
idx = 0
res = True
for key in test_dict:
if test_dict[key] ! = sub_list[idx]:
res = False
break
idx + = 1
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
test_dict = { "gfg" : 4 , "is" : 10 , "best" : 11 , "for" : 19 , "geeks" : 1 }
print ( "The original dictionary is : " + str (test_dict))
sub_list = [ 4 , 10 , 11 , 19 , 1 ]
res = list (test_dict.values()) = = sub_list
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
- 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
test_dict = { "gfg" : 4 , "is" : 10 , "best" : 11 }
print ( "The original dictionary is : " + str (test_dict))
sub_list = [ 4 , 11 , 10 ]
dict_values = list (test_dict.values())
sorted_dict_values = sorted (dict_values)
res = sorted_dict_values = = sub_list
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:
- 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
test_dict = { "gfg" : 4 , "is" : 10 , "best" : 11 , "for" : 19 , "geeks" : 1 }
print ( "The original dictionary is : " + str (test_dict))
sub_list = [ 4 , 10 , 11 , 19 , 1 ]
res = all (sub_list[i] = = v for i, v in enumerate (test_dict.values()))
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:
- 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
test_dict = { "gfg" : 4 , "is" : 10 , "best" : 11 , "for" : 19 , "geeks" : 1 }
print ( "The original dictionary is : " + str (test_dict))
sub_list = [ 4 , 10 , 11 , 19 , 1 ]
new_list = list ( map ( lambda x: test_dict[x], test_dict))
res = new_list = = sub_list
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Apr, 2023
Like Article
Save Article