GeeksforGeeks App
Open App
Browser
Continue

Python | Sum two unequal length lists in cyclic manner

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.

Python3

 `# Python code to add two different``# length lists in cyclic manner` `# Importing``from` `itertools ``import` `cycle` `# List initialization``list1 ``=` `[``150``, ``177``, ``454``, ``126``]``list2 ``=` `[``9``, ``44``, ``2``, ``168``, ``66``, ``80``, ``123``, ``6``, ``180``, ``184``]` `# Using zip``output ``=` `[x ``+` `y ``for` `x, y ``in` `zip``(cycle(list1), list2)]` `# Printing output``print``(output)`

Output:

`[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]`

Method #2: Using Iteratools and starmap

Python3

 `# Python code to add two different``# length lists in cyclic manner` `# Importing``from` `itertools ``import` `starmap, cycle``from` `operator ``import` `add` `# List initialization``list1 ``=` `[``150``, ``177``, ``454``, ``126``]``list2 ``=` `[``9``, ``44``, ``2``, ``168``, ``66``, ``80``, ``123``, ``6``, ``180``, ``184``]` `# Using starmap``output ``=` `list``(starmap(add, ``zip``(cycle(list1), list2)))` `# Print output``print``(output)`

Output:

`[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]`

Method #3: Using List comprehension

Python3

 `# Python code to add two different``# length lists in cyclic manner` `# List initialization``list1 ``=` `[``150``, ``177``, ``454``, ``126``]``list2 ``=` `[``9``, ``44``, ``2``, ``168``, ``66``, ``80``, ``123``, ``6``, ``180``, ``184``]` `# List comprehension``output ``=` `[list1[i ``%` `len``(list1)]``+``list2[i]``             ``for` `i ``in` `range``(``len``(list2))]` `# Printing output``print``(output)`

Output:

`[159, 221, 456, 294, 216, 257, 577, 132, 330, 361]`

Method #4: Using a for loop

Approach:

1. Initialize an empty list to store the output
2. Determine the length of the longer list and use it as the range for the for loop
3. In the for loop, add the elements from both lists cyclically and append the sum to the output list
4. 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:

1. Define a lambda function that takes two arguments x and y, and returns their sum.
2. Use the cycle() function from the itertools module to create an iterator that cycles through the elements of list1 indefinitely.
3. Use the map() function to apply the lambda function to each pair of corresponding elements from list1 and list2.
4. Convert the resulting map object to a list and assign it to output.
5. Print the output.

Python3

 `# Python code to add two different``# length lists in cyclic manner` `from` `itertools ``import` `cycle` `# List initialization``list1 ``=` `[``150``, ``177``, ``454``, ``126``]``list2 ``=` `[``9``, ``44``, ``2``, ``168``, ``66``, ``80``, ``123``, ``6``, ``180``, ``184``]` `# Using map and lambda``def` `add(x, y): ``return` `x ``+` `y` `# Adding 2 different length lists in cyclic manner``cycled_list1 ``=` `cycle(list1)``output ``=` `list``(``map``(add, cycled_list1, list2))` `# Printing output``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

1. Use numpy’s array and tile functions to create arrays of the same length by repeating the elements of the shorter array.
2. The max function is used to get the length of the longer array.
3. The arrays are then added element-wise using the + operator.
4. The tolist function is used to convert the numpy array back to a regular Python list.

Python3

 `import` `numpy as np` `# List initialization``list1 ``=` `[``150``, ``177``, ``454``, ``126``]``list2 ``=` `[``9``, ``44``, ``2``, ``168``, ``66``, ``80``, ``123``, ``6``, ``180``, ``184``]` `# Converting lists to numpy arrays``arr1 ``=` `np.array(list1)``arr2 ``=` `np.array(list2)` `# Getting the length of the longer array``n ``=` `max``(``len``(arr1), ``len``(arr2))` `# Reshaping the arrays to the same shape by``# repeating their elements``arr1 ``=` `np.tile(arr1, n``/``/``len``(arr1) ``+` `1``)[:n]``arr2 ``=` `np.tile(arr2, n``/``/``len``(arr2) ``+` `1``)[:n]` `# Adding the arrays element-wise``output ``=` `arr1 ``+` `arr2` `# Printing output``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.

My Personal Notes arrow_drop_up