Open In App

Sum of Consecutive Numbers with Overlapping in Lists

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of summing consecutive numbers with overlapping in a list involves iterating over the elements and adding each element to the next one, with the last element being paired with the first to form a circular sum. The goal is to efficiently compute the sum of consecutive elements while ensuring that the list “wraps around,” meaning that the first element is paired with the last. For example, consider the list a = [4, 7, 3, 2, 9, 2, 1]. The objective is to generate a new list where each element is the sum of itself and the next one, with the last element summing with the first. In this case, the output might look like [11, 10, 5, 11, 11, 3, 5] .

Using zip

zip() allows us to pair elements from two or more iterables. In this case, we can zip the original list with a shifted version of itself to add consecutive elements. By shifting the list with slicing, we can handle the circular nature of the overlap i.e., the last element sums with the first .

Python
li = [4, 7, 3, 2, 9, 2, 1]

res = [a + b for a, b in zip(li, li[1:] + [li[0]])]
print(str(res))

Output
[11, 10, 5, 11, 11, 3, 5]

Explanation: zip() pair each element in the list li with the next element, where the last element is paired with the first element using list concatenation (li[1:] + [li[0]]). The sum of each pair is computed in a list comprehension and stored in res.

Using itertools.isslice()

itertools.islice() allows for more efficient iteration over parts of a sequence without creating full copies, unlike list slicing. For sum of consecutive numbers, we can use islice to handle the shifted versions of the list.

Python
import itertools

li = [4, 7, 3, 2, 9, 2, 1]
res = [a + b for a, b in zip(li, list(itertools.islice(li, 1, None)) + list(itertools.islice(li, 0, 1)))]
print(str(res))

Output
[11, 10, 5, 11, 11, 3, 5]

Explanation: itertools.islice() create two slices of the list, one starting from the second element and the other containing the first element. These slices are concatenated and zip() pairs the original list with the shifted list, summing each pair.

Using deque()

deque designed for efficient appending and popping from both ends. We can use deque to rotate the list and sum consecutive numbers with overlap. This is especially efficient for rotating or shifting elements in the list.

Python
from collections import deque

li = [4, 7, 3, 2, 9, 2, 1]

deq = deque(li) # Convert the list into a deque
res = [deq[i] + deq[(i + 1) % len(deq)] for i in range(len(deq))]
print(str(res))

Output
[11, 10, 5, 11, 11, 3, 5]

Explanation:This code iterates over the deque, summing each element with the next, using modulo ((i + 1) % len(deq)) to ensure that the last element wraps around and is added to the first element.

Using loop

A simple for loop can manually iterate over the list, summing each element with its consecutive neighbor. This method provides explicit control over the iteration and making it ideal when performance tuning or custom behavior is required.

Python
a = [4, 7, 3, 2, 9, 2, 1]

n = len(a) # Get the length of the list
res = []  # Initialize an empty list

for i in range(n):
    res.append(a[i] + a[(i + 1) % n])
print(str(res))

Output
[11, 10, 5, 11, 11, 3, 5]

Explanation: This code iterates over the list a and adds each element to the next one using modulo ((i + 1) % n) to ensure that the last element wraps around and adds to the first element. The sum of each pair is appended to the res list .



Next Article
Practice Tags :

Similar Reads