Incremental List Extension in Python


The list is a popular datatype in Python that store multiple values in a single variable. While incrementing any number/string to the list it will use some operations and conditions to extend the list by adding new values. In Python, we have some built−in functions such as map(), range(), and, lambda will be used to solve the Incremental List Extension. Using list comprehension is another way to generate the expression, conditions, and, efficient result to solve this problem statements.

Syntax

The following syntax is used in the examples

map()

The built−in function map() allows a specific function for each element iteration.

range()

The range() is a built−in function in Python that sets the collection of new elements to the list.

lambda

The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda function only has one expression.

Using Nested list Comprehension

The program uses two nested lists by initializing two different variables and using list comprehension it allows the for loop to iterate through input list.

Example

In the following example, start the program by creating an input list in the variable str_inp. Then initialize two variables− n(to set the range for each index) and E(to set the extension). Next, it will use the first list comprehension to set the extension and range of each index by using for loop and built−in function range(), and these processes store in the variable temp. Now initialize variable temp to 0 which indicates the initial value for iteration in the next list comprehension. Then use the second list comprehension in the variable result. Finally, we are printing the output with the help of a variable named result.

str_inp = [10, 20, 30, 40]
n = 3 # Set the limit for a particular index
E = 2 # Extension
temp = [1 * E**i for i in range(n)]
temp[0] = 0
result = [ele + tele for ele in str_inp for tele in temp]
print("The incrementing list extension and addition:", result)

Output

The incrementing list extension and addition: [10, 12, 14, 20, 22, 24, 30, 32, 34, 40, 42, 44]

Using map() and Lambda

The program uses some built−in functions such as list() to create a list object, map() to allow specific iteration, and, lambda to calculate the list extension.

Example

In the following example, we will use the built−in nested structure of the Python function such as map() that accepts two parameters− lambda to calculate the extension of the particular element using for loop and input list to specify each index element. After processing these function it sets to a built−in function list() that convert the resultant value into the list form. Next, using list comprehension it will iterate all the elements for range and extension. Finally, display the program output

# input list
my_str = [1, 2, 3, 4, 5]
n = 2 # Range
E = 3 # Extension
# Using arithmetic operator in list comprehension
temp = [1 * E**i for i in range(n)]
temp[0] = 0
res = list(map(lambda elem: [elem + extn for extn in temp], my_str))
# list comprehension
result = [num for sublist in res for num in sublist]
print("The incrementing list extension and addition:", result)

Output

The incrementing list extension and addition: [1, 4, 2, 5, 3, 6, 4, 7, 5, 8]

Using itertools.product()

The program uses a Python module named itertools.product() which returns the cartesian product of input iterable and helps to generate the output based on incremental list extension.

Example

In the following example, we will start the program by importing the module named itertools that will be used in for loop iteration. Then initialize the value of the range and extension. There will be uses of two list comprehension-

i. First list comprehension− It will use multiplication and exponentiation to work on the range and extension of the given value.

ii. Second list comprehension− In this comprehension technique it will use two variables for the iteration using for loop with the help of built−in function itertools.product() that define the set of all ordered pair.

import itertools
my_list = [100, 200, 300]
n = 4 # Range
E = 3 # Extension
# First list comprehension
temp = [1 * E**i for i in range(n)]
temp[0] = 0
# Second list comprehension
res = [elem + total_elem for elem, total_elem in itertools.product(my_list, temp)]
print("The incrementing list extension and addition:", res)

Output

The incrementing list extension and addition: [100, 103, 109, 127, 200, 203, 209, 227, 300, 303, 309, 327]

Using Generator Expression

The program uses a generator expression that works similarly to list comprehension and it helps to increment the list extension.

Example

In the following example, we will use the two different arithmetic operators− multiplication(*) and exponentiation(**) to work on the iteration of range and extension using for loop of this program. Then using the generator expression it will target the particular index to work on range and extension. Next, display the result.

my_lst = [5, 10]
n = 5 # Range
E = 2 # Extension
temp = [1 * E**i for i in range(n)]
temp[0] = 0
# Iteration of given input list(generator expression)
result = (element + extension for element in my_lst for extension in temp)
print("The incrementing list extension and addition:", list(result))

Output

The incrementing list extension and addition: [5, 7, 9, 13, 21, 10, 12, 14, 18, 26]

Conclusion

We discussed the various method to solve the problem statement. Most of the example uses comprehension technique whereas one example has use the itertools module that defines the cartesian product between two variable.

Updated on: 14-Aug-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements