Are Two Dicts Equal?

Easy ⏱ 8 min 74% acceptance ★★★★☆ 4.4
Write a function dicts_equal(d1, d2) that returns True if d1 and d2 contain exactly the same keys mapped to exactly the same values (order does not matter), and False otherwise. Do not use the == operator directly in your return statement — implement the comparison manually using keys and values.

Examples

Example 1
Input
dicts_equal({'a': 1, 'b': 2}, {'b': 2, 'a': 1})
Output
True
Explanation

Same key-value pairs, different insertion order.

Example 2
Input
dicts_equal({'a': 1}, {'a': 1, 'b': 2})
Output
False
Explanation

Different key sets.

Constraints

  • Do not simply `return d1 == d2`.

Topics

Dictionariesequality

Companies

IBMHCLTech

Hints

Hint 1

First compare `d1.keys() == d2.keys()`, then check every value matches.

Hint 2

Or compare set of items(): `set(d1.items()) == set(d2.items())` if values are hashable.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.