Search code examples
list-comprehension

List issue in python : how to except one value before product of remaining all


I have a list of items. I want to replace first item with product of remaining items of list except first. Do the same for remaining all. How can i do that?

lst = [2,3,5,4,7]

Output should be :

New_lst = [420,280,168,210,120]

Solution

  • First get the product:

    >>> import math
    >>> p = math.prod([2,3,5,4,7])
    >>> p
    840
    

    Then divide all numbers by the product:

    >>> lst = [2,3,5,4,7]
    >>> New_lst = [p//i for i in lst]
    >>> New_lst
    [420, 280, 168, 210, 120]