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


Dictionary is the mutable data structure in python, which allows the user to store the data in the format of key and value. The key and value are separated by using the symbol ":". The keys are unique and the values can be repeated. The values can be given as a single element or multiple elements within a list. If we want to access the elements from a dictionary we have to use the key. It provides various functions and methods to work and manipulate the dictionaries.

There are several approaches to check whether the values of a dictionary are in same order as in a list.

Comparing List and Dictionary Values

The zip() function is a built-in Python function that takes multiple iterables as arguments and returns an iterator of tuples. Each tuple contains the corresponding elements from the input iterables. In other words, the zip() function groups the elements from multiple iterables together based on their positions.

List comprehension is a concise and powerful way to create lists in Python. It allows us to generate a new list by iterating over an existing iterable such as a list, tuple, or string and applying an expression or condition to each element. The resulting list is created in a single line of code, making the syntax clean and easy to understand.

Example

In this approach, we use the zip() function along with the list comprehension. The zip() function to iterate through both the list and dictionary simultaneously. The zip() function returns an iterator that generates tuples containing corresponding elements from each iterable.

Next we will use a list comprehension within the all() function to compare each value from the dictionary dict_values with the corresponding element from the list list_values. The all() function returns True only if all comparisons are True. If the lengths of the list and dictionary are not equal, the iteration will stop at the shorter length, and the extra values in the longer container will be ignored.

def check_order(list_values, dict_values):
   return all(list_val == dict_val for list_val, dict_val in zip(list_values, dict_values))
my_list = [1, 2, 3, 4, 5]
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
result = check_order(my_list, my_dict.values())
print(result)

Output

True

Converting Dictionary to a list of values

In this approach, we convert the dictionary values into a list and then compare it directly with the given list.

Example

In this approach, we convert the dictionary values dict_values.values() into a list using the list() function. We then compare this list with the given list directly using the == operator. If the two lists are equal, it means that the values of the dictionary are in the same order as in the list.

def check_order(list_values, dict_values):
   return list(dict_values.values()) == list_values
my_list = [1, 2, 3, 4, 5, 6]
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
result = check_order(my_list, my_dict)
print(result)

Output

False

Updated on: 02-Aug-2023

41 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements