Python program to check whether the values of a dictionary are in same order as in a list
Last Updated :
09 Apr, 2023
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.
Similar Reads
Python Program to display keys with same values in a dictionary List
Given a list with all dictionary elements, the task is to write a Python program to extract keys having similar values across all dictionaries. Examples: Input : test_list = [{"Gfg": 5, "is" : 8, "best" : 0}, {"Gfg": 5, "is" : 1, "best" : 0}, {"Gfg": 5, "is" : 0, "best" : 0}] Output : ['Gfg', 'best'
5 min read
Python program to Sort a List of Dictionaries by the Sum of their Values
Given Dictionary List, sort by summation of their values. Input : test_list = [{1 : 3, 4 : 5, 3 : 5}, {1 : 100}, {8 : 9, 7 : 3}] Output : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 100}] Explanation : 12 < 13 < 100, sorted by values sum Input : test_list = [{1 : 100}, {8 : 9, 7 : 3}] Output : [{8:
6 min read
Python Program to check whether Characters of all string elements are in lexical order or not
Given a list with string elements, the task is to write a Python program to return True if all list elements are sorted. The point of caution here is to keep in mind we are comparing characters of a string list element with other characters of the same list element and not string elements with each
4 min read
Python program to find Maximum value from dictionary whose key is present in the list
Given a list with dictionary keys and a dictionary, extract maximum from dictionary values, whose key is present in list. Examples: Input : test_dict = {"Gfg": 4, "is" : 5, "best" : 10, "for" : 11, "geeks" : 3}, test_list = ["Gfg", "best", "geeks"] Output : 10 Explanation : Max value is 11, but not
6 min read
Python program to group keys with similar values in a dictionary
Given a dictionary with values as a list. Group all the keys together with similar values. Input : test_dict = {"Gfg": [5, 6], "is": [8, 6, 9], "best": [10, 9], "for": [5, 2], "geeks": [19]} Output : [['Gfg', 'is', 'for'], ['is', 'Gfg', 'best'], ['best', 'is'], ['for', 'Gfg']] Explanation : Gfg has
2 min read
Python - Test if all Values are Same in Dictionary
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
7 min read
Sort a List of Dictionaries by a Value of the Dictionary - Python
We are given a list of dictionaries where each dictionary contains multiple key-value pairs and our task is to sort this list based on the value of a specific key. For example, Given the list: students = [{'name': 'David', 'score': 85}, {'name': 'Sophia', 'score': 92}, {'name': 'Ethan', 'score': 78}
2 min read
Check if one dictionary is subset of other - Python
Checking if one dictionary is a subset of another involves verifying whether all key-value pairs of the smaller dictionary exist in the larger dictionary with the same values. For example, given two dictionaries a = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5} and b = {'gfg': 1, 'is': 2, 'best'
4 min read
Python - Check if all values are K in dictionary
While working with dictionary, we might come to a problem in which we require to ensure that all the values are K in dictionary. This kind of problem can occur while checking the status of the start or checking for a bug/action that could have occurred. Letâs discuss certain ways in which this task
9 min read
Python | Check if all values are 0 in dictionary
While working with dictionary, we might come to a problem in which we require to ensure that all the values are 0 in dictionary. This kind of problem can occur while checking status of start or checking for a bug/action that could have occurred. Let's discuss certain ways in which this task can be p
7 min read