# 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 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
```

Method #2 : Using `islice()` + loop
The inbuilt funtions 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 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
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.