Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Sliced Product in List

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 : Using list slicing + loop 
This problem can be performed in 1 line rather than using a loop using the list slicing functionality provided by Python and then using a loop to perform the product.

Python3




# Python3 code to demonstrate
# Sliced Product in List
# using list slicing + loop
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 5
 
# Sliced Product in List
# using list slicing + loop
res = prod(test_list[:K])
 
# print result
print("The first K elements product of list is : " + str(res))


Output : 

The original list : [4, 5, 2, 6, 7, 8, 10]
The first K elements product of list is : 1680

 

Time complexity: O(n), where n is the length of the test_list. The list slicing + loop  takes O(n) time
Auxiliary Space: O(1), constant extra space is required

 
Method #2 : Using islice() + loop 
The inbuilt functions can also be used to perform this particular task. The islice function can be used to get the sliced list and then prod() can be employed to perform product.
 

Python3




# Python3 code to demonstrate
# Sliced Product in List
# using islice() + loop
from itertools import islice
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 5
 
# Sliced Product in List
# using islice() + loop
res = list(islice(test_list, 0, K))
res = prod(res)
 
# print result
print("The first K elements product of list is : " + str(res))


Output : 

The original list : [4, 5, 2, 6, 7, 8, 10]
The first K elements product of list is : 1680

 

Time Complexity: O(n), where n is the length of the input list. This is because we’re using islice() + loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), constant extra space is required

Method #3 : Using numpy()

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

Python3




# Python3 code to demonstrate
# Sliced Product in List
# using numpy
import numpy as np
  
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing K
K = 5
  
# Sliced Product in List
# using numpy
res = np.prod(test_list[:K])
  
# print result
print("The first K elements product of list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list : [4, 5, 2, 6, 7, 8, 10]
The first K elements product of list is : 1680

Time complexity: O(n)

Auxiliary space: O(1)

Method #4 : Using operator.getitem(),slice() methods

Approach

  1. Slice the given test_list using operator.getitem(),slice() and initialise a variable res=1
  2. Initiate a for loop to traverse the sliced list and multiply each element to res using *= operator
  3. Display res where product is stored

Python3




# Python3 code to demonstrate
# Sliced Product in List
# using list slicing + loop
 
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 5
 
# Sliced Product in List
# using list slicing + loop
res=1
import operator
x=operator.getitem(test_list,slice(0,K))
for i in x:
    res*=i
 
# print result
print("The first K elements product of list is : " + str(res))


Output

The original list : [4, 5, 2, 6, 7, 8, 10]
The first K elements product of list is : 1680

Time Complexity : O(K) K – length of sliced list

Auxiliary Space : O(1) since we used only one variable to store product.

Method5: Using Recursive method: The idea to use the recursive method is based on the following two conditions: 

  1. If K is 0, return 1 (base case)
  2. Otherwise, return the product of the last element in the first K elements and the result of the function called with the first K-1 elements (recursive case)

Python3




# Python program for the above approach
 
# Recursive function to find product
# of first K elements in a list
def sliced_product(lst, K):
    if K == 0:
        return 1
    else:
        return lst[K-1] * sliced_product(lst, K-1)
 
 
# Driver Code
test_list = [4, 5, 2, 6, 7, 8, 10]
K = 5
 
print("The original list : " + str(test_list))
 
res = sliced_product(test_list[:K], K)
print("The first K elements product of list is : " + str(res))


Output

The original list : [4, 5, 2, 6, 7, 8, 10]
The first K elements product of list is : 1680

Time Complexity:
The recursive function calls itself K times, with each call reducing K by 1 until it reaches the base case where K equals 0. Therefore, the time complexity of the function is O(K).

Space Complexity:
The function uses recursion to solve the problem, so it creates a new stack frame for each recursive call. The maximum depth of the stack is K, so the space complexity of the function is also O(K).

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments