Albert
Albert

Reputation: 1

How to make tuples of nums list?

I currently have:

def product_of_tuples(nums_list):
    '''Receives a list of tuples of two or more numbers. Returns
    a list of the products of the numbers in each tuple. (Note:
    the product of a sequence of numbers is obtained by multiplying
    them together.)'''

    result = []
    for numbers in nums_list:
        *# ''' need something here '''*
        for num in numbers:
            multi_number = num * num 
        result.append(multi_number)
    return result

When running

print(product_of_tuples([(1, 5), (6, 1), (2, 3, 4)])) the expected output should be [5, 6, 24]

Any suggestion would be appreciated :)

Upvotes: 0

Views: 102

Answers (3)

Mauro Baraldi
Mauro Baraldi

Reputation: 6575

from operator import mul

def product_of_tuples(nums_list):
    '''Receives a list of tuples of two or more numbers. Returns
    a list of the products of the numbers in each tuple. (Note:
    the product of a sequence of numbers is obtained by multiplying
    them together.)'''

    return [reduce(mul, i) for i in nums_list]

Upvotes: 1

CloudTuan
CloudTuan

Reputation: 91

I think you should multiply the number in each tuple one by one but num * num which gives square. I can try this:

def product_of_tuples(nums_list):
result = []
for tuple in nums_list:
    s = 1
    for item in tuple:
        s *= item
    result.append(s)
return result

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

You goofed up the insides. Since this is multiplication you need to reset the accumulator to 1 each time, and then you need to multiply it by each number in turn.

Upvotes: 1

Related Questions