Issue
I have the list named as input_list = [1, 2, 3, 4, 5]
, I want to take the product of every elements except the specific index which is running at the moment.
Example
input_list1 = [1, 2, 3, 4, 5]
If I say that in the first index the value is 1 , in the output list, its product will be the all elements except first index (5*4*3*2)
and insert it into new list, same goes for all elements.
Expected_Output:
prod = [120, 60, 40, 30, 24]
Solution
You can use math.prod
for the task:
import math
out = [
math.prod(input_list1[:i] + input_list1[i + 1 :])
for i in range(len(input_list1))
]
print(out)
Prints:
[120, 60, 40, 30, 24]
Answered By - Andrej Kesely Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.