Python – Incremental List Extension
Last Updated :
26 Feb, 2025
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.
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.
Similar Reads
Python - Incremental Slice concatenation in String list
Given a String list, perform the task of the concatenation of strings by increasing the size of each string. Examples: Input : test_list = ['gfg', 'for', 'all', 'geeks'], Output : gfoallgeek Explanation : g, fo, all, geek -> concatenated from each string [ increasing order ].Input : test_list = [
4 min read
Python - Elements Lengths in List
Sometimes, while working with Python lists, can have problem in which we need to count the sizes of elements that are part of lists. This is because list can allow different element types to be its members. This kind of problem can have application in many domains such has day-day programming and we
6 min read
Python | List expansion by K
Sometimes, we require to reduce the length of the python list but other times we might also require to increase its size, and that too by repeating every element N times. This type of utility can come in day-day programming. Let's discuss certain ways in which this can be achieved. Method # : Using
6 min read
Python | N element incremental tuples
Sometimes, while working with data, we can have a problem in which we require to gather a data that is of the form of sequence of increasing element tuple with each tuple containing the element N times. Let's discuss certain ways in which this task can be performed. Method #1 : Using generator expre
5 min read
Insert list in another list - Python
We are given two lists, and our task is to insert the elements of the second list into the first list at a specific position. For example, given the lists a = [1, 2, 3, 4] and b = [5, 6], we want to insert list 'b' into 'a' at a certain position, resulting in the combined list a = [1, 2, 5, 6, 3, 4]
4 min read
Python | Increment 1's in list based on pattern
Given a list of binary numbers 0 and 1, Write a Python program to transform the list in such a way that whenever 1 appears after the occurrence of a sequence of 0's, increment it by n+1, where 'n' is the last increment. Examples: Input : [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1] Output : [0, 1, 0, 0, 2,
2 min read
Python - Incremental value initialization in Dictionary
The interconversion between the datatypes is very popular and hence many articles have been written to demonstrate different kind of problems with their solutions. This article deals with yet another similar type problem of converting a list to dictionary, with values as the index incremental with K
5 min read
Python | Interval Initialization in list
Interval Initialization in list means creating a list in Python where specific intervals are initialized with certain values. There are numerous ways to initialize the list with the elements, but sometimes, its required to initialize the lists with the numbers in a sliced way. This can be custom and
4 min read
Python | Find elements of a list by indices
Given two lists with elements and indices, write a Python program to find elements of list 1 at indices present in list 2. Examples: Input : lst1 = [10, 20, 30, 40, 50] lst2 = [0, 2, 4] Output : [10, 30, 50] Explanation: Output elements at indices 0, 2 and 4 i.e 10, 30 and 50 respectively. Input : l
5 min read
Python - Test for Incrementing Dictionary
Given a dictionary, test if it is incrementing, i.e. its key and values are increasing by 1. Input : test_dict = {1:2, 3:4, 5:6, 7:8} Output : True Explanation : All keys and values in order differ by 1. Input : test_dict = {1:2, 3:10, 5:6, 7:8} Output : False Explanation : Irregular items. Method 1
6 min read