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
Finding the Summation of Nested Record Values in Python
The problem statement requires finding the summation of nested record values using Python. Sometimes we need to add values present in nested dictionaries, which is useful for data manipulation and aggregation tasks using dictionary keys.
Understanding the Problem
We need to find the addition of values in nested records (dictionaries). A nested dictionary is a dictionary containing other dictionaries as values. Our goal is to find nested values and sum them appropriately.
Method 1: Sum All Values in Nested Records
This approach recursively traverses the nested dictionary and sums all numeric values ?
Algorithm
Step 1 Define a function that takes a nested dictionary as input
Step 2 Initialize a sum variable to store the total
Step 3 Loop through all values in the dictionary
Step 4 If the value is numeric, add it to sum. If it's a nested dictionary, recursively call the function
Example
# Function to get the summation of all values
def sum_record_values(the_record):
total_sum = 0
for value in the_record.values():
if isinstance(value, int):
total_sum += value
elif isinstance(value, dict):
total_sum += sum_record_values(value)
return total_sum
# Input nested record
the_record = {
'A': 1,
'B': {
'C': 5,
'D': {
'E': 6,
'F': 5
}
},
'G': 7
}
# Call the function and get result
output = sum_record_values(the_record)
print("The summation of nested record values:", output)
The summation of nested record values: 24
Time Complexity: O(N), where N is the total number of key-value pairs in all nested levels.
Method 2: Sum Values by Identical Keys
This approach sums values that share the same key across different nested dictionaries ?
Algorithm
Step 1 Initialize the nested record structure
Step 2 Create an empty dictionary to store sum results
Step 3 Loop through each sub-dictionary
Step 4 For each key-value pair, add the value to the corresponding key in the result dictionary
Example
# Initialize the nested record
the_record = {
'Vegetable': {'red': 4, 'green': 5, 'yellow': 8},
'Fruits': {'red': 8, 'yellow': 10},
'Dry_fruits': {'yellow': 19, 'green': 10}
}
# Initialize empty dictionary for results
record_values = {}
# Sum values by identical keys
for subdict in the_record.values():
for key, item in subdict.items():
record_values[key] = item + record_values.get(key, 0)
# Display the results
print("The summation values:", record_values)
The summation values: {'red': 12, 'green': 15, 'yellow': 37}
Time Complexity: O(n×m), where n is the number of outer keys and m is the average number of inner keys.
Comparison
| Method | Use Case | Output Type |
|---|---|---|
| Sum All Values | Total of all numeric values | Single number |
| Sum by Identical Keys | Aggregate values by key names | Dictionary with sums |
Conclusion
We have successfully implemented two approaches for summing nested record values in Python. Use the first method for total summation and the second method when you need to aggregate values by identical keys across nested structures.
