PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How can we take out the product of elements?

 May 09, 2022     list, product, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing