Open In App

Python | Arbitrary List Product

Improve
Improve
Like Article
Like
Save
Share
Report

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 requirement to perform this in most concise manner. Let us discuss certain ways in which this can be done. 

Method #1 : Using list comprehension + randrange() + loop 

The naive method to perform this particular task can be shortened using list comprehension. randrange function is used to perform the task of generating random numbers. The task of performing product is done using loop. 

Python3




# Python3 code to demonstrate
# Arbitrary List Product
# using list comprehension + randrange() + loop
import random
 
# getting Product
 
 
def prod(val):
    res = 1
    for ele in val:
        res *= ele
    return res
 
 
# using list comprehension + randrange() + loop
# Arbitrary List Product
res = prod([random.randrange(1, 50, 1) for i in range(7)])
 
# printing result
print("Arbitrary number product list is : " + str(res))


Output : 

Arbitrary number product list is : 1182384000

Method #2: Using random.sample() + loop

This single utility function performs the exact required as asked by the problem statement, it generated N no. of arbitrary numbers in a list in the specified range and returns the required list. The task of performing the product is done using loop.

Example:

Python3




# Python3 code to demonstrate
# Arbitrary List Product
# using random.sample() + loop
import random
 
# getting Product
 
 
def prod(val):
    res = 1
    for ele in val:
        res *= ele
    return res
 
 
# using random.sample() + loop
# Arbitrary List Product
res = prod(random.sample(range(1, 50), 7))
 
# printing result
print("Arbitrary number product list is : " + str(res))


Output : 

Arbitrary number product list is : 1182384000

 Method #3: Using Reduce() 

This code defines a function arbitrary_list_product() that takes a list lst as its argument and returns the product of all elements in the list. The function uses the reduce() function from the functools module to apply a lambda function that multiplies two elements to each pair of elements in the list, resulting in a single value that represents the product of all elements. This product is then returned as the output of the function.

Python3




# Import the reduce function from the functools module
from functools import reduce
 
# Define a function that uses the reduce function to calculate the product of a list of numbers
 
 
def arbitrary_list_product_reduce(lst):
    # Use the reduce function to apply a lambda function that multiplies two elements to each pair of elements in the list
    return reduce(lambda x, y: x * y, lst)
 
 
# Create a list of integers
my_list = [1, 2, 3, 4, 5]
 
# Call the reduce-based function to calculate the product of the list
product_reduce = arbitrary_list_product_reduce(my_list)
 
# Print the results to the console
print(product_reduce)  # Output: 120


Output

120

Time complexity: O(n)
Auxiliary Space: O(1)

Method #4: using NumPy

Algorithm:

  1. Accept a list of integers as input.
  2. Call the np.prod() function from the NumPy library, passing in the input list as an argument.
  3. Return the result of the np.prod() function.

Python3




import numpy as np
 
 
def product_numpy(lst):
    return np.prod(lst)
 
 
# Example usage
my_list = [1, 2, 3, 4, 5]
result = product_numpy(my_list)
print(result)
# This code is contributed by Jyothi pinjala.


Output: 

120

Time complexity:

The np.prod() function used in this code has a time complexity of O(n), where n is the number of elements in the input list. This is because it needs to iterate over each element in the list to calculate the product.
The product_numpy() function itself has a time complexity of O(1), as it simply calls the np.prod() function with the input list.

Auxiliary Space:

The product_numpy() function has a space complexity of O(1), as it only uses a small amount of memory to store the input list and the result of the np.prod() function. The np.prod() function also has a space complexity of O(1), as it doesn’t create any new data structures or use any additional memory beyond the input list.

Method #5: Using recursion to calculate the product of a list of numbers

Python3




def arbitrary_list_product_recursion(lst):
    # Base case: if the list is empty, return 1
    if not lst:
        return 1
    # Recursive case: multiply the first element of the list by the product of the rest of the list
    return lst[0] * arbitrary_list_product_recursion(lst[1:])
 
# Create a list of integers
my_list = [1, 2, 3, 4, 5]
 
# Call the recursion-based function to calculate the product of the list
product_recursion = arbitrary_list_product_recursion(my_list)
 
# Print the results to the console
print(product_recursion) # Output: 120


Output

120

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), due to the recursive function calls on the stack.



Last Updated : 25 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads