Comparing dictionaries based on a combination of keys
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When comparing Python dictionaries based on a subset of their keys, you extract the relevant keys from each dictionary and compare only those values. This is useful for matching records that share an identity (like name + date) but may differ in other fields. Approaches include extracting key subsets with comprehensions, using operator.itemgetter, converting to tuples for hashing, and using pandas for large-scale record matching.
Comparing Two Dictionaries on Specific Keys
Extracting a Key Subset
Finding Matching Records in Two Lists
Finding Differences Between Dictionaries
Grouping by Composite Keys
Deduplicating by Composite Key
Using Pandas for Large-Scale Comparison
Common Pitfalls
- Using unhashable values as composite keys: Tuple keys like
(d["name"], d["list_field"])fail if any value is unhashable (lists, dicts). Convert unhashable values to strings or frozensets before creating the key tuple:tuple(str(d[k]) for k in keys). - Forgetting about missing keys: If a dictionary is missing one of the comparison keys,
d[k]raisesKeyError. Used.get(k)which returnsNonefor missing keys, or validate that all required keys exist before comparing. - Case-sensitive string comparison:
{"name": "Alice"}and{"name": "alice"}are not equal. If case-insensitive matching is needed, normalize values first:d.get(k, "").lower()for string keys. - Comparing floating-point values: Direct equality (
==) on float values may fail due to precision. Usemath.isclose(d1[k], d2[k])for floating-point comparisons, or round to a fixed number of decimal places before comparing. - O(n*m) performance with nested loops: Comparing every record in list A against every record in list B is O(n*m). Convert one list to a set of key tuples first for O(n+m) performance, or use pandas merge for large datasets.
Summary
- Compare dictionaries on specific keys with
all(d1.get(k) == d2.get(k) for k in keys) - Extract key subsets with
{k: d[k] for k in keys}for partial dictionary comparison - Convert composite keys to tuples for O(1) set lookups and efficient record matching
- Use
collections.defaultdictfor grouping records by composite keys - Use
pandas.merge(on=[keys])for efficient large-scale record matching and comparison - Handle edge cases: missing keys (use
.get()), case sensitivity, and unhashable values
Related reading
- Comparing object graph representation to adjacency list and matrix representations
- Comparing scikit learn clusterings using a decision tree
- Comparing two byte arrays in .NET
- Comparing two collections for equality irrespective of the order of items in them
- Comparing two dictionaries and checking how many key, value pairs are equal
- Comparing two NumPy arrays for equality, element-wise
- Comparing unordered_map vs unordered_set
- Complete Weighted Graph and Hamiltonian Tour

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.