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

Python dictionaries store data as key-value pairs where keys are unique but values can be duplicated. Sometimes we need to check whether dictionary values appear in the same order as elements in a list. Python provides several approaches to accomplish this comparison efficiently.

Using zip() with List Comprehension

The zip() function pairs corresponding elements from multiple iterables, while list comprehension provides a concise way to create lists. Combined with all(), we can compare elements position by position ?

Example

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(f"Same order: {result}")

# Test with different order
my_dict2 = {'x': 2, 'y': 1, 'z': 3, 'w': 4, 'v': 5}
result2 = check_order(my_list, my_dict2.values())
print(f"Different order: {result2}")
Same order: True
Different order: False

The zip() function stops when the shorter iterable is exhausted, so extra elements in longer containers are ignored.

Converting Dictionary Values to List

This approach converts dictionary values to a list and performs direct comparison using the equality operator ?

Example

def check_order_direct(list_values, dict_values):
    return list(dict_values.values()) == list_values

my_list = [1, 2, 3, 4, 5]
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

result = check_order_direct(my_list, my_dict)
print(f"Same order and length: {result}")

# Test with different lengths
my_list2 = [1, 2, 3, 4, 5, 6]
result2 = check_order_direct(my_list2, my_dict)
print(f"Different lengths: {result2}")
Same order and length: True
Different lengths: False

Using enumerate() for Index-Based Comparison

This method checks each dictionary value against the list element at the same index ?

Example

def check_order_enumerate(list_values, dict_values):
    dict_vals = list(dict_values.values())
    
    if len(list_values) != len(dict_vals):
        return False
    
    for i, list_val in enumerate(list_values):
        if list_val != dict_vals[i]:
            return False
    return True

my_list = [10, 20, 30]
my_dict = {'first': 10, 'second': 20, 'third': 30}

result = check_order_enumerate(my_list, my_dict)
print(f"Order matches: {result}")

# Test with mixed data types
my_list2 = ['a', 'b', 'c']
my_dict2 = {'x': 'a', 'y': 'b', 'z': 'c'}
result2 = check_order_enumerate(my_list2, my_dict2)
print(f"String values match: {result2}")
Order matches: True
String values match: True

Comparison

Method Length Check Memory Usage Best For
zip() + all() Ignores length difference Low (iterator) Large datasets
Direct list comparison Strict length matching Medium (creates list) Simple comparisons
enumerate() Custom length handling Medium Custom validation logic

Conclusion

Use zip() with all() for memory-efficient comparison of large datasets. Use direct list comparison for simple cases where exact length matching is required. The enumerate() approach offers the most flexibility for custom validation logic.

Updated on: 2026-03-27T10:50:09+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements