Open In App

Python – Incremental List Extension

Last Updated : 26 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a list, a = [7, 8, 9], extend it N times its original size such that each expansion step adds increasing values based on powers of M. The first expansion step (i = 0) keeps the original list unchanged, while subsequent steps add M^i to each element. For example,

For a = [7, 8, 9], N = 4, M = 3:

Compute increments: [0, 3, 9, 27] (i.e., M^i for i in range N).

Add each increment to all elements of a:

0: [7, 8, 9]

3: [10, 11, 12]

9: [16, 17, 18]

27: [34, 35, 36]

Final Output: [7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]

Let’s discuss some of the efficient ways of doing this in Python.

Using numpy

We can use NumPy for efficient list expansion. The original list ‘a’ is converted into a NumPy array, and incremented list is generated as a sequence of powers of M, with the first element set to 0. Using np.add.outer(), we compute all pairwise sums between elements of a and increments, then flatten the result into a sorted list.

Python
import numpy as np

a = [7, 8, 9]
N = 4
M = 3

# Create numpy array from list
arr = np.array(a)

# Create correct increments: [0, 3, 9, 27]
increments = np.power(M, np.arange(N))
increments[0] = 0  # Ensure the first element is 0

# Use broadcasting to add increments correctly
result = np.add.outer(arr, increments).flatten('F')  # Flatten column-wise (Fortran order)

# Convert result back to list
combinations = result.tolist()

print(combinations)

Output
[7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]

Explanation:

  • np.power(M, np.arange(N)) generates the sequence [1, 3, 9, 27], but increments[0] = 0 ensures it starts with 0, giving [0, 3, 9, 27].
  • np.add.outer(arr, increments) efficiently computes pairwise sums between elements of a and increments.
  • result.flatten(‘F’) ensures correct row-wise ordering by flattening in Fortran (column-major) order.
  • tolist() converts the NumPy array into a standard Python list.

Using list comprehension 

We can also use list comprehension to do the same task. The process involves two key steps: first, creating a helper list to generate the incremental addition factors, and then applying these factors to the original list to construct the final extended sequence.

Python
a = [7, 8, 9]
N = 4
M = 3

# Incremental List Extension
temp = [M**i if i > 0 else 0 for i in range(N)]

res = list([i + j for j in temp for i in a])

print(str(res))

Output
[7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]

Explanation:

  • temp = [0, 3, 9, 27] is generated using [M**i if i > 0 else 0 for i in range(N)], with the first element set to 0.
  • List comprehension [i + j for j in temp for i in a] efficiently computes all pairwise sums.

Using map() and zip() functions

We can achieve the result using map() and zip(). First, we generate an incremental sequence using list comprehension. The original list is extended by repeating its elements and aligning them with sorted increments. Using zip(), we pair corresponding values, and map() with a lambda function applies element-wise addition to form the final expanded list.

Python
a = [7, 8, 9]
N = 4
M = 3

# Generate incremental values
temp = [M**i if i > 0 else 0 for i in range(N)]

# Use map() and zip() to apply increments
res = list(map(lambda x: x[0] + x[1], zip(a * N, sorted(temp * len(a)))))

print(res)

Output
[7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]

Explanation:

  • sorted(temp * len(a)) duplicates temp to match the length of a * N and sorts it, producing [0, 0, 0, 3, 3, 3, 9, 9, 9, 27, 27, 27], ensuring correct alignment for element-wise addition.
  • zip(a * N, …) pairs each repeated element of a with the corresponding value from the sorted increments, maintaining the correct order.
  • map() with a lambda function performs element-wise addition, combining values from zip() to generate the final expanded list and list() function converts it to a list.

Using itertools library

We generate an incremental sequence in temp like we did previously an then using itertools.product(), we create all possible pairs (x, y), where x is from a and y is from temp, and then sum each pair to form the final extended list and sort the result in increasing order using sorted() function.

Python
from itertools import product

a = [7, 8, 9]
N = 4
M = 3

# Generate list of powers of M
temp = [M**i if i > 0 else 0 for i in range(N)]

# Generate all combinations using itertools.product
res = sorted([x + y for x, y in product(a, temp)])

print(res)

Output
[7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]

Explanation:

  • product(a, temp) creates all pairs (x, y), combining each element of a with every element of temp.
  • [x + y for x, y in product(a, temp)] adds corresponding pairs to form the expanded list, which is then sorted using sorted().

Using nested loops

First, we generate an incremental sequence in temp, where each element follows a multiplication pattern. Then, using two loops, we iterate over temp and the original list, adding each element of a to every value in temp, extending the list in a structured manner.

Python
a = [7, 8, 9]
N = 4
M = 3

temp = [M**i for i in range(N)]
temp[0] = 0

res = []
for j in temp:
    for i in a:
        res.append(i + j)

print(str(res))

Output
[7, 8, 9, 10, 11, 12, 16, 17, 18, 34, 35, 36]

Explanation: temp = [0, 3, 9, 27] is generated by [M**i for i in range(N)], with the first element set to 0. The nested loops add each element of a to every element in temp, extending ‘res’ incrementally.



Next Article
Practice Tags :

Similar Reads