Python - Chuncked summation every K value


Chunked sum also known as partial sum or rolling sum, which is a process of calculating the sum of elements in a sequence such as list, array, or any iterable, in smaller chunks or subsets rather than calculating the sum of the entire sequence at once. Each chunk represents a group of consecutive elements from the sequence, and the sum is calculated for each chunk individually.

For example, consider the sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], and let's calculate the chunked sum with a chunk size of 3.

  • Chunk 1: [1, 2, 3] → Sum: 1 + 2 + 3 = 6

  • Chunk 2: [4, 5, 6] → Sum: 4 + 5 + 6 = 15

  • Chunk 3: [7, 8, 9] → Sum: 7 + 8 + 9 = 24

  • Chunk 4: [10] → Sum: 10

The result of the chunked sum with a chunk size of 3 would be [6, 15, 24, 10].

Chunked sum can be useful in various scenarios, such as processing large datasets or time series data in chunks to avoid memory-related issues and improve computational efficiency. It is also commonly used in algorithms and data analysis tasks that require processing data in small segments.

To perform chunked summation in Python, where we calculate the sum of every K values in a list or iterable, we can use various approaches.

Using Loops

One straightforward approach is to use a loop to iterate over the list or iterable and calculate the sum of every K values.

Example

In this example, we are initializing an empty list result to store the chunked sums. We also initialize chunk_sum to keep track of the sum of each chunk. We iterate over the values using enumerate to get both the index i and value. We are adding each value to chunk_sum and check if i is a multiple of K. If it is, we append chunk_sum to the result list and reset chunk_sum to 0. Finally, we check if there are any remaining values that didn't form a complete chunk and append them to the result list if needed.

def chunked_sum_loop(values, K):
   result = []
   chunk_sum = 0
   for i, value in enumerate(values, 1):
      chunk_sum += value
      if i % K == 0:
         result.append(chunk_sum)
         chunk_sum = 0
   if chunk_sum != 0:
      result.append(chunk_sum)
   return result
values = [5,78,787,99,44,9,22]
K = 2
print("The chuncked sum of the given values:",chunked_sum_loop(values, K))

Output

The chuncked sum of the given values: [83, 886, 53, 22]

Using List Comprehension

Another approach is utilizing list comprehension to generate a list of chunked sums.

Example

In this example, we are using a list comprehension to iterate over the values in chunks of size K using the range function. For each chunk, we calculate the sum using the sum() function, resulting in a list of chunked sums.

def chunked_sum_list_comprehension(values, K):
   return [sum(values[i:i+K]) for i in range(0, len(values), K)]
values = [5,78,787,99,44,9,22]
K = 2
print("The chuncked sum of the given values:",chunked_sum_list_comprehension (values, K))

Output

The chuncked sum of the given values: [83, 886, 53, 22]

Using itertools.islice()

The islice() function from the itertools module provides an efficient way to extract chunks from an iterable.

Example

In this example we are creating an iterator from the values using iter() and initialize an empty result list. We continuously extract chunks of size K using islice() and convert them to a list. If the extracted chunk is empty, it means we have reached the end of the values, so we will break the loop. Otherwise, we will calculate the sum of the chunk using sum() and append it to the result list.

from itertools import islice
def chunked_sum_islice(values, K):
   iterator = iter(values)
   result = []
   while True:
      chunk = list(islice(iterator, K))
      if not chunk:
         break
      result.append(sum(chunk))
   return result
values = [5,78,7,8,9,9,4,4,9,22]
K = 2
print("The sum of the given values:",chunked_sum_islice(values, K))

Output

The sum of the given values: [83, 15, 18, 8, 31]

Updated on: 07-Aug-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements