Python | Sum two unequal length lists in cyclic manner
Last Updated :
26 Apr, 2023
Given two unequal-length lists, the task is to add elements of two list such that when elements of the smaller list are over, add elements in a circular manner till all element of the larger list is iterated. Let’s discuss different ways we can do this task.
Method #1: Using Iteratools and zip
Python3
from itertools import cycle
list1 = [ 150 , 177 , 454 , 126 ]
list2 = [ 9 , 44 , 2 , 168 , 66 , 80 , 123 , 6 , 180 , 184 ]
output = [x + y for x, y in zip (cycle(list1), list2)]
print (output)
|
Output:
[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]
Method #2: Using Iteratools and starmap
Python3
from itertools import starmap, cycle
from operator import add
list1 = [ 150 , 177 , 454 , 126 ]
list2 = [ 9 , 44 , 2 , 168 , 66 , 80 , 123 , 6 , 180 , 184 ]
output = list (starmap(add, zip (cycle(list1), list2)))
print (output)
|
Output:
[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]
Method #3: Using List comprehension
Python3
list1 = [ 150 , 177 , 454 , 126 ]
list2 = [ 9 , 44 , 2 , 168 , 66 , 80 , 123 , 6 , 180 , 184 ]
output = [list1[i % len (list1)] + list2[i]
for i in range ( len (list2))]
print (output)
|
Output:
[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]
Method #4: Using a for loop
Approach:
- Initialize an empty list to store the output
- Determine the length of the longer list and use it as the range for the for loop
- In the for loop, add the elements from both lists cyclically and append the sum to the output list
- Print the output list
Below is the implementation of the above approach:
Python3
list1 = [ 150 , 177 , 454 , 126 ]
list2 = [ 9 , 44 , 2 , 168 , 66 , 80 , 123 , 6 , 180 , 184 ]
output = []
longer_length = max ( len (list1), len (list2))
for i in range (longer_length):
sum = list1[i % len (list1)] + list2[i % len (list2)]
output.append( sum )
print (output)
|
Output
[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]
Time complexity: O(max(len(list1), len(list2)))
Auxiliary space: O(max(len(list1), len(list2))) for the output list
Method 5: Using the built-in map() function and lambda function.
Approach:
- Define a lambda function that takes two arguments x and y, and returns their sum.
- Use the cycle() function from the itertools module to create an iterator that cycles through the elements of list1 indefinitely.
- Use the map() function to apply the lambda function to each pair of corresponding elements from list1 and list2.
- Convert the resulting map object to a list and assign it to output.
- Print the output.
Python3
from itertools import cycle
list1 = [ 150 , 177 , 454 , 126 ]
list2 = [ 9 , 44 , 2 , 168 , 66 , 80 , 123 , 6 , 180 , 184 ]
def add(x, y): return x + y
cycled_list1 = cycle(list1)
output = list ( map (add, cycled_list1, list2))
print (output)
|
Output
[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]
Time complexity: O(n), where n is the length of the longer input list, because we need to iterate over all the elements of the longer list once.
Auxiliary space: O(n), because we create a new list to store the output.
Method 6: Using numpy
- Use numpy’s array and tile functions to create arrays of the same length by repeating the elements of the shorter array.
- The max function is used to get the length of the longer array.
- The arrays are then added element-wise using the + operator.
- The tolist function is used to convert the numpy array back to a regular Python list.
Python3
import numpy as np
list1 = [ 150 , 177 , 454 , 126 ]
list2 = [ 9 , 44 , 2 , 168 , 66 , 80 , 123 , 6 , 180 , 184 ]
arr1 = np.array(list1)
arr2 = np.array(list2)
n = max ( len (arr1), len (arr2))
arr1 = np.tile(arr1, n / / len (arr1) + 1 )[:n]
arr2 = np.tile(arr2, n / / len (arr2) + 1 )[:n]
output = arr1 + arr2
print (output.tolist())
|
Output
[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]
Time complexity: O(n), where n is the length of the longer list (since we need to iterate over all elements of the longer list).
Auxiliary space: O(n), since we create two numpy arrays of length n.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...