Python - Cubes Product in list
Last Updated :
16 Feb, 2023
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of cubes of list in just one line. Let’s discuss certain ways in which this can be performed.
Method #1: Using reduce() + lambda The power of lambda functions to perform lengthy tasks in just one line, allows it combined with reduce which is used to accumulate the subproblem, to perform this task as well. Works with only Python 2.
Python
# Python code to demonstrate
# Cubes Product in list
# using reduce() + lambda
# initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print ("The original list is : " + str(test_list))
# using reduce() + lambda
# Cubes Product in list
res = reduce(lambda i, j: i * j*j * j, [test_list[:1][0]**3]+test_list[1:])
# printing result
print ("The product of cubes of list is : " + str(res))
OutputThe original list is : [3, 5, 7, 9, 11]
The product of cubes of list is : 1123242379875
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using map() + loop The similar solution can also be obtained using the map function to integrate and external product function to perform the product of the cubed number.
Python3
# Python3 code to demonstrate
# Cubes Product in list
# using loop + max()
# getting Product
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print ("The original list is : " + str(test_list))
# using loop + max()
# Cubes Product in list
res = prod(map(lambda i : i * i * i, test_list))
# printing result
print ("The product of cubes of list is : " + str(res))
OutputThe original list is : [3, 5, 7, 9, 11]
The product of cubes of list is : 1123242379875
Time complexity: O(n), where n is the number of items in the list, because it has to traverse the list once to calculate the cube of each element and multiply the results.
Auxiliary space: O(1), because it only uses a constant amount of extra space to store the result.
Method #3: Using math.pow()
Python3
# Python code to demonstrate
# Cubes Product in list
# initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print ("The original list is : " + str(test_list))
# Cubes Product in list
res = 1
for i in test_list:
import math
res*=math.pow(i,3)
res=int(res)
# printing result
print ("The product of cubes of list is : " + str(res))
OutputThe original list is : [3, 5, 7, 9, 11]
The product of cubes of list is : 1123242379875
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4: Using functools.reduce() and operator.mul
Python3
# Python code to demonstrate
# Cubes Product in list
# initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print ("The original list is : " + str(test_list))
# Cubes Product in list
x=[]
for i in test_list:
x.append(i**3)
from functools import reduce
import operator
res=reduce(operator.mul,x,1)
# printing result
print ("The product of cubes of list is : " + str(res))
OutputThe original list is : [3, 5, 7, 9, 11]
The product of cubes of list is : 1123242379875
Time Complexity : O(N)
Auxiliary Space : O(N)
Approach using numpy
Note: Install numpy module using command "pip install numpy"
Python3
import numpy as np
# Initializing list
test_list = [3, 5, 7, 9, 11]
# Printing original list
print("The original list is : ", test_list)
# Cubes product in list
result = np.prod(np.power(test_list, 3))
# Printing result
print("The product of cubes of list is : ", result)
Output:
The original list is : [3, 5, 7, 9, 11]
The product of cubes of list is : 1123242379875
Time Complexity: O(n)
Auxiliary Space: O(1)
The numpy library provides the prod() function which can be used to calculate the product of all elements in an array, and the power() function which can be used to raise the elements in the list to a specified power. In this case, we raise each element to the power of 3 and then calculate the product of the resulting list using the prod() function.
Method #6: Using list comprehension:
Python3
test_list = [3, 5, 7, 9, 11]
print("The original list is : " + str(test_list))
result = 1
for ele in [ele**3 for ele in test_list]:
result *= ele
print("The product of cubes of list is : " + str(result))
#This code is contributed by Jyothi pinjala.
OutputThe original list is : [3, 5, 7, 9, 11]
The product of cubes of list is : 1123242379875
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 7: Using simple for loop
Python3
# initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print("The original list is : " + str(test_list))
# initializing result
res = 1
# calculating product of cubes using a for loop
for i in test_list:
res *= i**3
# printing result
print("The product of cubes of list is : " + str(res))
OutputThe original list is : [3, 5, 7, 9, 11]
The product of cubes of list is : 1123242379875
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Python - Product of i^k in List
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of i^k of list in just one line. Letâs discuss
5 min read
Python - Sum of Cubes in List
We are having a list we need to find sum of cubes of list. For example, n = [1, 2, 3, 4] we are given this list we need to find sum of cubes of each element of list so that resultant output should be 100.Using List Comprehension with sum()List comprehension with sum() allows us to compute sum of cub
2 min read
Python | Arbitrary List Product
Sometimes, in making programs for gaming or gambling, we come across the task of creating a list with arbitrary numbers and performing its product. This task has to perform in general using a loop and appending the arbitrary numbers one by one and then performing the product. But there is always a r
5 min read
Python | Sliced Product in List
Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must know to perform the same. This article discusses ways to fetch the initial K elements and do its multiplication. Letâs discuss a certain solution to perform this task. Method #1
5 min read
Python - Suffix Product in list
We are given a list of numbers, and our task is to compute the suffix product for each position in the list. The suffix product at any index is the product of all elements to the right, including the element at that index. For example, given a = [2, 3, 4, 5], the suffix products would be [120, 60, 2
2 min read
Python | Column Product in List of lists
Sometimes, we are encountered with such problem in which we need to find the product of each column in a matrix i.e product of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Letâs discuss certain ways in which this problem can be solved. Meth
6 min read
List Product Excluding Duplicates - Python
The task of finding the product of unique elements in a list involves identifying and multiplying only the distinct values, effectively excluding any duplicates. For example, if a = [1, 3, 5, 6, 3, 5, 6, 1], the unique elements would be {1, 3, 5, 6}, and the product of these values would be 90. Usin
3 min read
Python - Kth Column Product in Tuple List
Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the product of itâs Kth index. This problem has application in web development domain while working with data informations. Letâs discuss certain ways in which this task can be performed. M
7 min read
Python | Product of Prefix in list
Nowadays, especially in competitive programming, the utility of computing prefix product is quite popular and features in many problems. Hence, having a one-liner solution to it would possess a great help. Letâs discuss certain ways in which this problem can be solved. Method 1: Using list comprehen
4 min read
Python - Product of consecutive pairs in list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be solved.
7 min read