How to Extract Dictionaries with Maximum Key Values in Python
When working with a list of dictionaries, a common task is to find the maximum value for each key across all dictionaries and then extract the dictionaries that contain those maximum values. This is useful in data analysis, leaderboard systems, performance tracking, and any scenario where you need to identify records that hold the "best" value for any given metric.
In this guide, you will learn multiple approaches to solve this problem - from extracting the full dictionaries that contain maximum values to simply finding the maximum value per key.
Understanding the Problem
Given a list of dictionaries with the same keys:
data = [
{"TutorialReference": 3, "is": 7, "Best": 8},
{"TutorialReference": 9, "is": 2, "Best": 9},
{"TutorialReference": 5, "is": 4, "Best": 10},
{"TutorialReference": 3, "is": 6, "Best": 14},
]
For each key, find its maximum value across all dictionaries:
"TutorialReference": max is 9 (in dict at index 1)"is": max is 7 (in dict at index 0)"Best": max is 14 (in dict at index 3)
Then extract all dictionaries that contain any of these maximum values:
[
{"TutorialReference": 3, "is": 7, "Best": 8}, # has max "is" (7)
{"TutorialReference": 9, "is": 2, "Best": 9}, # has max "TutorialReference" (9)
{"TutorialReference": 3, "is": 6, "Best": 14}, # has max "Best" (14)
]
Method 1: Find Maximum Values Per Key, Then Filter Dictionaries (Recommended)
This approach first determines the maximum value for each key, then filters dictionaries that contain any maximum value:
data = [
{"TutorialReference": 3, "is": 7, "Best": 8},
{"TutorialReference": 9, "is": 2, "Best": 9},
{"TutorialReference": 5, "is": 4, "Best": 10},
{"TutorialReference": 3, "is": 6, "Best": 14},
]
# Step 1: Find the maximum value for each key
keys = data[0].keys()
max_values = {key: max(d[key] for d in data) for key in keys}
print("Maximum values per key:", max_values)
# Step 2: Extract dictionaries that contain any maximum value
result = [
d for d in data
if any(d[key] == max_values[key] for key in keys)
]
print("Dictionaries with maximum values:")
for d in result:
print(f" {d}")
Output:
Maximum values per key: {'TutorialReference': 9, 'is': 7, 'Best': 14}
Dictionaries with maximum values:
{'TutorialReference': 3, 'is': 7, 'Best': 8}
{'TutorialReference': 9, 'is': 2, 'Best': 9}
{'TutorialReference': 3, 'is': 6, 'Best': 14}
How it works:
- A dictionary comprehension builds
max_valuesmapping each key to its maximum value across all dictionaries. - A list comprehension filters dictionaries where any key's value matches its maximum.
This approach is clean, efficient, and clearly separates the two concerns: finding maximums and filtering. It runs in O(n × k) time where n is the number of dictionaries and k is the number of keys.
Method 2: Using max() and filter() - Grouped by Key
If you need results grouped by key (which dictionaries hold the maximum for each specific key):
data = [
{"TutorialReference": 3, "is": 7, "Best": 8},
{"TutorialReference": 9, "is": 2, "Best": 9},
{"TutorialReference": 5, "is": 4, "Best": 10},
{"TutorialReference": 3, "is": 6, "Best": 8},
]
keys = data[0].keys()
result = {}
for key in keys:
max_val = max(d[key] for d in data)
result[key] = [d for d in data if d[key] == max_val]
for key, dicts in result.items():
print(f"Key '{key}' (max={max(d[key] for d in dicts)}):")
for d in dicts:
print(f" {d}")
Output:
Key 'TutorialReference' (max=9):
{'TutorialReference': 9, 'is': 2, 'Best': 9}
Key 'is' (max=7):
{'TutorialReference': 3, 'is': 7, 'Best': 8}
Key 'Best' (max=10):
{'TutorialReference': 5, 'is': 4, 'Best': 10}
Notice that multiple dictionaries can share the maximum value for a key. For example, if two dictionaries both have "Best": 10, both would appear in the results for the "Best" key.
Method 3: Extract Only the Maximum Values
If you only need the maximum value for each key (without the full dictionaries):
data = [
{"TutorialReference": 3, "is": 7, "Best": 8},
{"TutorialReference": 9, "is": 2, "Best": 9},
{"TutorialReference": 5, "is": 4, "Best": 10},
{"TutorialReference": 3, "is": 6, "Best": 8},
]
max_values = {key: max(d[key] for d in data) for key in data[0]}
print("Maximum values:", max_values)
Output:
Maximum values: {'TutorialReference': 9, 'is': 7, 'Best': 10}
Method 4: Using a Loop with Tracking
An explicit loop approach that tracks maximums and their source dictionaries simultaneously:
data = [
{"TutorialReference": 3, "is": 7, "Best": 8},
{"TutorialReference": 9, "is": 2, "Best": 9},
{"TutorialReference": 5, "is": 4, "Best": 10},
{"TutorialReference": 3, "is": 6, "Best": 14},
]
# Track maximum value and corresponding dictionaries for each key
max_tracker = {}
for d in data:
for key, value in d.items():
if key not in max_tracker or value > max_tracker[key]["max"]:
max_tracker[key] = {"max": value, "dicts": [d]}
elif value == max_tracker[key]["max"]:
max_tracker[key]["dicts"].append(d)
# Collect unique dictionaries that hold any maximum
seen = set()
result = []
for info in max_tracker.values():
for d in info["dicts"]:
dict_id = id(d)
if dict_id not in seen:
seen.add(dict_id)
result.append(d)
print("Dictionaries with maximum values:")
for d in result:
print(f" {d}")
Output:
Dictionaries with maximum values:
{'TutorialReference': 9, 'is': 2, 'Best': 9}
{'TutorialReference': 3, 'is': 7, 'Best': 8}
{'TutorialReference': 3, 'is': 6, 'Best': 14}
This approach processes the data in a single pass to find maximums, making it efficient for very large datasets.
Common Mistake: Assuming All Dictionaries Have the Same Keys
If dictionaries have different keys, accessing a missing key raises a KeyError:
Problem:
data = [
{"TutorialReference": 3, "is": 7},
{"TutorialReference": 9, "Best": 9}, # Missing "is" key
]
keys = data[0].keys()
max_values = {key: max(d[key] for d in data) for key in keys}
# KeyError: 'is' (because second dict doesn't have this key)
Fix - use .get() with a default value:
data = [
{"TutorialReference": 3, "is": 7},
{"TutorialReference": 9, "Best": 9},
]
# Collect all unique keys across all dictionaries
all_keys = set().union(*data)
max_values = {
key: max(d.get(key, float("-inf")) for d in data)
for key in all_keys
}
print(max_values)
Output:
{'Best': 9, 'is': 7, 'TutorialReference': 9}
Always verify that all dictionaries share the same keys before comparing values across them. Use set().union(*data) to collect all keys and .get(key, default) to safely handle missing keys.
Practical Example: Finding Top Performers
Here's a real-world scenario - finding employees who hold the top score in any category:
employees = [
{"name": "Alice", "sales": 150, "support": 95, "coding": 88},
{"name": "Bob", "sales": 120, "support": 98, "coding": 92},
{"name": "Carol", "sales": 180, "support": 85, "coding": 95},
{"name": "Dave", "sales": 90, "support": 92, "coding": 88},
]
# Find max for each metric (excluding "name")
metric_keys = [k for k in employees[0] if k != "name"]
max_values = {key: max(e[key] for e in employees) for key in metric_keys}
# Find top performers
top_performers = [
e for e in employees
if any(e[key] == max_values[key] for key in metric_keys)
]
print("Maximum values per metric:", max_values)
print("\nTop performers (best in at least one category):")
for emp in top_performers:
reasons = [f"{k}={emp[k]}" for k in metric_keys if emp[k] == max_values[k]]
print(f" {emp['name']}: best in {', '.join(reasons)}")
Output:
Maximum values per metric: {'sales': 180, 'support': 98, 'coding': 95}
Top performers (best in at least one category):
Bob: best in support=98
Carol: best in sales=180, coding=95
Comparison of Approaches
| Approach | Output Format | Handles Ties | Time Complexity | Best For |
|---|---|---|---|---|
| Max values + filter | Flat list of dicts | ✅ Yes | O(n × k) | General use (recommended) |
| Grouped by key | Dict of key → list of dicts | ✅ Yes | O(n × k) | Per-key analysis |
| Max values only | Dict of key → max value | N/A | O(n × k) | Quick summary |
| Single-pass loop | Flat list of dicts | ✅ Yes | O(n × k) | Large datasets |
Summary
Extracting dictionaries with maximum key values from a list involves two steps: finding the maximum value per key and filtering dictionaries accordingly. Key takeaways:
- Find max values first using a dictionary comprehension with
max(), then filter withany()to find dictionaries containing any maximum. - Group by key when you need to know which dictionaries are the best for each specific key.
- Handle missing keys with
.get(key, default)when dictionaries might not all share the same keys. - Handle ties - multiple dictionaries can share the same maximum value for a key; include all of them in your results.
- Use the single-pass approach for very large datasets where minimizing iterations matters.