0

I am trying to write a function (for academic reasons) that multiply every number in this list : list = [6 , [1, 2, 3]] I have tried this solution:

def grow(arr):
    for num in arr:
        x = num
        x * num
        for value in num:
            y = value * num
        return y * x   

But I keep getting: TypeError: 'int' object is not iterable someone help me understand please. The expected output should be: 36. The function should do this: 6 * 1 * 2 * 3

4 Answers4

2

You can try:

def flatten(li):
    try:
        for item in li:
            yield from flatten(item)
    except TypeError:
        yield li

li = [6,[1, 2, 3]]
l = [fi for fi in flatten(li)]
reduce(lambda x,y: x*y, l)

36
Pygirl
  • 12,969
  • 5
  • 30
  • 43
1

Or something similar with one of the answer which first flattned the nested list using the flatten and using numpy prod to calculate the cross multiplication.

import numpy  as np
def flatten(li):
    try:
        for item in li:
            yield from flatten(item)
    except TypeError:
        yield li

li = [6,[1, 2, 3]]
list1 = [fi for fi in flatten(li)]

result1 = np.prod(list1)

Initially, the OP accept the suggestion per the code below

Extract the nested list

num, val_list = [6 , [1, 2, 3]]

multiply and add all the value in the list

expected_output=sum([value * num for value in val_list])

As a function:

def grow(arr):
 num, val_list = arr
 return sum([value * num for value in val_list])


my_arr= [6 , [1, 2, 3]]
data=grow(my_arr)
mpx
  • 3,081
  • 2
  • 26
  • 56
  • Your answer looks like the right one but I am having trouble in turning into a function can you help me? – Davi Cheli Miquelim Feb 24 '21 at 05:05
  • and you also made a wrong sum call but your solution is the cleanest – Davi Cheli Miquelim Feb 24 '21 at 05:10
  • what do you mean by wrong sum call? – mpx Feb 24 '21 at 05:13
  • Have you tested your code? – Davi Cheli Miquelim Feb 24 '21 at 05:18
  • Thanks a lot for the help, but it did not work. I got a ValueError – Davi Cheli Miquelim Feb 24 '21 at 05:20
  • can you try again. Also, in your question please edit the list from `[[6 , [1, 2, 3]]` to `[6 , [1, 2, 3]]` – mpx Feb 24 '21 at 05:21
  • I get the exact output in my compiler. Also, the first two lines indicate that the you get what is originally requested – mpx Feb 24 '21 at 05:26
  • can you try print(data) – mpx Feb 24 '21 at 05:29
  • In the question it is mentioned, what is to be done is `6*1*2*3` but I think this code is doing something like `(6*1)+(6*2)+(6*3)`. Though the answers match in this very case, other cases will generate different results. – theProcrastinator Feb 24 '21 at 06:07
  • `[4 , [0, 2, 3]]` should give 0 but it is giving `20` as @YashvanderBamel has pointed out. The logic is totally wrong Plus `num, val_list = ....` Solution should be general. not specific to a single case. – Pygirl Feb 24 '21 at 06:12
  • Please note that, the OP has made the modification after the I suggested this approach. Prior that, I drafted the code based on the text `expected output should be: 36`. – mpx Feb 26 '21 at 06:52
  • Please note that, the OP has made the modification after the suggestion I made. Prior to that, I drafted the code based on the text `expected output should be: 36` – mpx Feb 26 '21 at 09:44
1

If you're pretty sure that arr will always contain just two entries like [6, [1, 2, 3]] the above solutions are good to go. But what if someone supplied something like this - [1, [2, 3, 4], 5, 6, [2, 4, 5]]. Provided all the values are integers you can do the following:

def grow(arr):
    temp = list()
    for i in arr:
        if type(i) == int:
            temp.append(i)
        else:
            temp.extend(i)

    return temp

Now that all the elements have been taken into the new list temp, you can import math and do math.prod(temp)

0
def grow(arr):
    for num in arr:
        x = num
        x * num
        for value in num:
            y = value * num
        return y * x

When your outer for loop iterates, it finds two elements in the list: 6 and [1, 2, 3]. therefore, the first time through that loop, num is 6.

Therefore, your inner for resolves to

        for value in 6:

Which is not possible. Instead, you need to take the two list elements in sequence, not as a nested loop.

num, val_list = arr
for value in val_list:
    y = value * num

That fixes your immediate problem. I'll leave the other errors up to you. You still have to store all three products; in the loop above, you overwrite the first two with the last one.

Prune
  • 76,765
  • 14
  • 60
  • 81