Python | Sliced Product in List
Last Updated :
22 Apr, 2023
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
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
K = 5
res = prod(test_list[: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: 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
from itertools import islice
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
K = 5
res = list (islice(test_list, 0 , K))
res = prod(res)
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
import numpy as np
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
K = 5
res = np.prod(test_list[: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: O(n)
Auxiliary space: O(1)
Method #4 : Using operator.getitem(),slice() methods
Approach
- Slice the given test_list using operator.getitem(),slice() and initialise a variable res=1
- Initiate a for loop to traverse the sliced list and multiply each element to res using *= operator
- Display res where product is stored
Python3
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
K = 5
res = 1
import operator
x = operator.getitem(test_list, slice ( 0 ,K))
for i in x:
res * = i
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:
- If K is 0, return 1 (base case)
- 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
def sliced_product(lst, K):
if K = = 0 :
return 1
else :
return lst[K - 1 ] * sliced_product(lst, K - 1 )
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).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...