# Python program to find product of given number of consecutive elements

Given a List, the task is to write a python program that can construct a list with products of consecutive elements for a given number of elements.

Input : test_list = [5, 6, 2, 1, 7, 5, 3], K = 3

Output : [60, 12, 14, 35, 105]

Explanation : 5 * 6 * 2 = 60, 6 * 2 * 1 = 12.. And so on.

Input : test_list = [5, 6, 2, 1, 7, 5, 3], K = 4

Output : [60, 84, 70, 105]

Explanation : 5 * 6 * 2 * 1 = 60, 6 * 2 * 1 * 7 = 84.. And so on.

Method 1 : Using list slicing and loop

In this, we perform task of getting K slice using list slicing and task of getting product is done by an external function call.

Example:

## Python3

 `# getting product` `def` `prod(sub):` `    ``res ``=` `1` `    ``for` `ele ``in` `sub:` `        ``res ``=` `ele ``*` `res` `    ``return` `res`     `# initializing lists` `test_list ``=` `[``5``, ``6``, ``2``, ``1``, ``7``, ``5``, ``3``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# initializing K` `K ``=` `3`   `res ``=` `[]` `for` `idx ``in` `range``(``len``(test_list) ``-` `K ``+` `1``):`   `    ``# getting product using external function` `    ``res.append(prod(test_list[idx: idx ``+` `K]))`   `# printing result` `print``(``"Computed Products : "` `+` `str``(res))`

Output:

The original list is : [5, 6, 2, 1, 7, 5, 3]

Computed Products : [60, 12, 14, 35, 105]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2 : Using generator, slicing, reduce() and mul operator

In this, generator is used to compute and return intermediate result. Task of getting sliced multiplication is done using inbuilt function reduce(), and mul operator.

Example:

## Python3

 `from` `functools ``import` `reduce` `from` `operator ``import` `mul`   `# generator function`     `def` `sliced_prod(sub, K):` `    ``for` `idx ``in` `range``(``len``(sub) ``-` `K ``+` `1``):`   `        ``# slicing and returning intermediate product` `        ``sliced ``=` `sub[idx: idx ``+` `K]` `        ``yield` `reduce``(mul, sliced)`   `# generator function`     `# initializing lists` `test_list ``=` `[``5``, ``6``, ``2``, ``1``, ``7``, ``5``, ``3``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# initializing K` `K ``=` `3`   `# calling fnc.` `res ``=` `list``(sliced_prod(test_list, K))`   `# printing result` `print``(``"Computed Products : "` `+` `str``(res))`

Output:

The original list is : [5, 6, 2, 1, 7, 5, 3]

Computed Products : [60, 12, 14, 35, 105]

Time complexity: O(n), where n is the length of the test_list. The generator, slicing, reduce() and mul operator takes O(n) time to create the final list.
Auxiliary Space: O(1), extra space required is not required

Method 3 :  Use the numpy library

## Python3

 `import` `numpy as np`   `# initializing lists` `test_list ``=` `[``5``, ``6``, ``2``, ``1``, ``7``, ``5``, ``3``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# initializing K` `K ``=` `3`   `# using numpy library` `result ``=` `np.prod(np.array([test_list[i:i``+``K] ``for` `i ``in` `range``(``len``(test_list) ``-` `K ``+` `1``)]), axis``=``1``)`   `# printing result` `print``(``"Computed Products : "` `+` `str``(result))`

```OUTPUT :
The original list is : [5, 6, 2, 1, 7, 5, 3]
Computed Products : [ 60  12  14  35 105]```

Time complexity: O(NK), where N is the length of the input list test_list and K is the size of the sliding window.
Auxiliary space: O(NK). We are storing all the intermediate products in an array before computing their product.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next