Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Constant Multiplication over List

Python – Constant Multiplication over List

While working with the python lists, we can come over a situation in which we require to multiply constant to each element in the list. We possibly need to iterate and multiply constant to each element but that would increase the line of code. Let’s discuss certain shorthands to perform this task.

Method #1 : Using List Comprehension

List comprehension is just the short way to perform the task we perform using the naive method. This is mainly useful to save time and also is best among others when it comes to the readability of the code.

step-by-step approach: 

  1. First, the program initializes a list called test_list with the following elements: [4, 5, 6, 3, 9].
  2. Then, it prints out the original list using the print function and string concatenation: “The original list is : ” + str(test_list).
  3. After that, it initializes a variable called K with the value 4.
  4. The program uses a list comprehension to create a new list called res that multiplies each element of the test_list by the constant K. The list comprehension is written as [x * K for x in test_list].
  5. Finally, the program prints out the resulting list res using the print function and string concatenation: “The list after constant multiplication : ” + str(res).

Python3




# Python3 code to demonstrate
# Constant Multiplication over List
# using list comprehension
 
# initializing list
test_list = [4, 5, 6, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# using list comprehension
# Constant Multiplication over List
res = [x * K for x in test_list]
 
# printing result
print ("The list after constant multiplication : " + str(res))


Output

The original list is : [4, 5, 6, 3, 9]
The list after constant multiplication : [16, 20, 24, 12, 36]

Time Complexity: O(n), The above code iterates through the list once, hence the time complexity is linear, i.e. O(n).
Auxiliary Space: O(n), The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).

Method #2 : Using map() + operator.mul 

This is similar to the above function but uses the operator.mul to multiply each element to other element from the other list of K formed before applying the map function. It multiplies the similar index elements of list.

Python3




# Python3 code to demonstrate
# Constant Multiplication over List
# using map() + operator.mul
import operator
 
# initializing list
test_list = [4, 5, 6, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K list
K_list = [4] * len(test_list)
 
# using map() + operator.mul
# Constant Multiplication over List
res = list(map(operator.mul, test_list, K_list))
 
# printing result
print ("The list after constant multiplication : " + str(res))


Output

The original list is : [4, 5, 6, 3, 9]
The list after constant multiplication : [16, 20, 24, 12, 36]

Time complexity: The time complexity of this approach is O(n), where n is the length of the input list. The map() function runs in O(n) time, and the operator.mul function also takes constant time.

Auxiliary space: The auxiliary space required by this approach is O(n) because we are creating a new list to store the result of the multiplication operation.

Method #3 : Using numpy

Note: Install numpy module using command “pip install numpy”

Another approach to perform constant multiplication over a list is by using numpy library.

Python3




import numpy as np
 
# Python program to perform constant multiplication over a list
 
test_list = [4, 5, 6, 3, 9]
K = 4
 
# Using numpy
result = list(np.array(test_list) * K)
 
# printing result
print("The list after constant multiplication :", result)
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The list after constant multiplication : [16, 20, 24, 12, 36] 

Time complexity: O(n) as it is iterating through the list once.
 Auxiliary Space: O(n) as it is creating a new list with multiplied values.
 

Method 4 : using a for loop to iterate through each element in the list and multiplying it by the constant K.

 Here is the step-by-step approach:

  1. Define a list called test_list containing the values [4, 5, 6, 3, 9].
  2. Define a constant K with a value of 4.
  3. Create an empty list called result.
  4. Use a for loop to iterate over the indices of the test_list list, from 0 to len(test_list) – 1.
  5. Within the for loop, multiply the value at the current index of test_list by the constant K, and append the result to the result list.
  6. Once the for loop has finished, the result list will contain the values [16, 20, 24, 12, 36].
  7. Use the print() function to display the resulting list with a message. The output will be “The list after constant multiplication : [16, 20, 24, 12, 36]”.

Python3




# Python program to perform constant multiplication over a list
 
test_list = [4, 5, 6, 3, 9]
K = 4
 
# Using for loop
result = []
for i in range(len(test_list)):
    result.append(test_list[i] * K)
 
# printing result
print("The list after constant multiplication :", result)


Output

The list after constant multiplication : [16, 20, 24, 12, 36]

Time complexity: O(n), where n is the number of elements in the list, since we need to iterate through each element once.
Auxiliary space: O(n), since we are creating a new list to store the multiplied elements.

Method 8: Using a generator expression and the map() function

Use a generator expression along with the map() function to perform constant multiplication over the list. Here’s how the code will look

  1. Initialize a list test_list with some integer values.
  2. Initialize a constant integer K to a value.
  3. Use map() function along with a lambda function and generator expression to perform constant multiplication over the list.
  4. The lambda function is applied to each element of the generator expression which generates elements of the input list one at a time.
  5. The lambda function takes each element of the list as input, multiplies it with the constant K, and returns the result.
  6. The map() function then applies the lambda function to each element of the generator expression and returns a generator object.
  7. Convert the generator object to a list using the list() function and store it in the variable result.
  8. Print the resulting list to the console using the print() function.

Python3




# Python program to perform constant multiplication over a list
 
test_list = [4, 5, 6, 3, 9]
K = 4
 
# Using a generator expression and the map() function
result = list(map(lambda x: x * K, (i for i in test_list)))
 
# printing result
print("The list after constant multiplication :", result)


Output

The list after constant multiplication : [16, 20, 24, 12, 36]

Time complexity: O(n)
Auxiliary space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments