Finding the Summation of Nested Record Values in Python


The problem statement requires finding the summation of nested record values using Python. Sometimes we are required to add the values present in the data so that time this approach can be useful as record is the powerful dataset in terms of updating or manipulating the data using the keys.

Understanding the Problem

The given problem is to find the addition of values in the given record so here we will be using the nested dictionary as a record. And we will implement the code using Python.

And the nested dictionaries are the dictionary inside the dictionary. And we have to find the nested values and sum them up as a result.

Algorithm - Sum all the Values Present in the Record

  • Step 1 − Define the function called sum_record_values and inside this function we will pass the nested dictionary called the_record as input.

  • Step 2 − Initialize the sum variable in which we will store the summation of all the values present in the nested record.

  • Step 3 − A loop will be initiated for all the key-value pairs in the nested record. And then the condition will be initiated to verify that the value is a number then add the value in the sum variable.

  • Step 4 − And If the integer number is in the nested record so that call the above created function recursively and add the returned value to the sum.

Example

# Function to get the summation of values
def sum_record_values (the_record):
   sum = 0
   for value in the_record.values():
      if isinstance(value, int):
         sum += value
      elif isinstance(value, dict):
         sum += sum_record_values(value)
   return sum
# input nested record
the_record = {
   'A': 1,
   'B': {
      'C': 5,
      'D': {
         'E': 6,
         'F': 5
      }
   },
   'G': 7
}

# pass the input record and call the function
output = sum_record_values(the_record)

# show the output
print("The summation of nested record values:", output)

Output

The summation of nested record values: 24

Complexity

The time complexity of the above function sum_record_values is O(N), N is the number of key-value pairs in the record.

Algorithm - Sum Identical Key Values in the Record

  • Step 1 − Initialize the nested record as the_record.

  • Step 2 − Then we will initialize the empty record to store the sum values with the identical keys.

  • Step 3 − Then the loop will be initiated for the values of the_record.

  • Step 4 − Inside the outer loop we will initiate another inner loop over the sub record items.

  • Step 5 − Then inside the inner loop the current item value will be added to the respective key in the sum_values record.

  • Step 6 − The updated sum_values record is printed with the help of print() function.

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 the empty record as dictionary
record_values = dict()
for subdict in the_record.values():
   for key, item in subdict.items():
      record_values[key] = item + record_values.get(key, 0)

#Show the summation of nested record values with same key
print("The summation values : " + str(record_values))

Output

The summation values : {'red': 12, 'green': 15, 'yellow': 37}

Complexity

The time taken by the above code for finding the summation of same or identical keys is O(nm). In this n is the number of keys in the outer record and m is the number of keys in the inner record.

Conclusion

So we have successfully implemented the codes for finding the summation of nested record values using Python. And we have seen two approaches in this article in first method we summed up all the values present in the record and in the second method we have summed up the identical key values.

Updated on: 16-Oct-2023

29 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements