Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python – Extract dictionaries with values sum greater than K
Sometimes we need to filter a list of dictionaries based on the sum of their values. This is useful when working with data where you want to find records whose total values exceed a certain threshold.
Using Loop Iteration
The most straightforward approach is to iterate through each dictionary and calculate the sum of its values ?
student_scores = [
{"Math": 14, "Science": 18, "English": 19},
{"Math": 12, "Science": 4, "English": 16},
{"Math": 13, "Science": 17, "English": 11},
{"Math": 13, "Science": 16, "English": 13}
]
print("The list of dictionaries:")
print(student_scores)
K = 35
print(f"Threshold K: {K}")
result = []
for dictionary in student_scores:
total = 0
for key in dictionary:
total += dictionary[key]
if total > K:
result.append(dictionary)
print("Dictionaries with sum greater than K:")
print(result)
The list of dictionaries:
[{'Math': 14, 'Science': 18, 'English': 19}, {'Math': 12, 'Science': 4, 'English': 16}, {'Math': 13, 'Science': 17, 'English': 11}, {'Math': 13, 'Science': 16, 'English': 13}]
Threshold K: 35
Dictionaries with sum greater than K:
[{'Math': 14, 'Science': 18, 'English': 19}, {'Math': 13, 'Science': 17, 'English': 11}, {'Math': 13, 'Science': 16, 'English': 13}]
Using sum() Function
Python's built-in sum() function provides a more concise way to calculate the total ?
student_scores = [
{"Math": 14, "Science": 18, "English": 19},
{"Math": 12, "Science": 4, "English": 16},
{"Math": 13, "Science": 17, "English": 11},
{"Math": 13, "Science": 16, "English": 13}
]
K = 35
result = []
for dictionary in student_scores:
if sum(dictionary.values()) > K:
result.append(dictionary)
print("Dictionaries with sum greater than K:")
print(result)
Dictionaries with sum greater than K:
[{'Math': 14, 'Science': 18, 'English': 19}, {'Math': 13, 'Science': 17, 'English': 11}, {'Math': 13, 'Science': 16, 'English': 13}]
Using List Comprehension
For a more Pythonic approach, use list comprehension to filter in one line ?
student_scores = [
{"Math": 14, "Science": 18, "English": 19},
{"Math": 12, "Science": 4, "English": 16},
{"Math": 13, "Science": 17, "English": 11},
{"Math": 13, "Science": 16, "English": 13}
]
K = 35
result = [d for d in student_scores if sum(d.values()) > K]
print("Dictionaries with sum greater than K:")
print(result)
print(f"Found {len(result)} dictionaries with sum > {K}")
Dictionaries with sum greater than K:
[{'Math': 14, 'Science': 18, 'English': 19}, {'Math': 13, 'Science': 17, 'English': 11}, {'Math': 13, 'Science': 16, 'English': 13}]
Found 3 dictionaries with sum > 35
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Manual Loop | Medium | Good | Learning/debugging |
| sum() Function | High | Better | Clean readable code |
| List Comprehension | High | Best | Concise filtering |
Conclusion
Use list comprehension with sum(d.values()) for the most Pythonic solution. The manual loop approach helps understand the logic, while sum() provides better readability than manual iteration.
Advertisements
