This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Anukul Sangwan
over 10 years

A proper solution to 3.3 product

def product(x):           #defining function
    x=list(x)       #the input x is a list
    y=1   #y is a variable with value 1, didn't choose 0 because..
    for i in x: #for every element of list x
        y*=i #y*product of all elements [y=1, not 0]
    return y

Answer 52c702027c82ca369c0002c1

32 votes

Permalink

I think it could be shorter.

 def product(lst):
    total = 1
    for i in lst:
        total *= i
    return total

Only 5 lines!

points
Submitted by Gottlieb Uahengo
about 10 years

5 comments

Jay_123 about 10 years

sorry but could you please explain what’s the reason and meaning for having “total = 1”

Ron B about 10 years

if you did “total = 0”, the the returned value would always be “0” because 0 times anything is…. ZERO.

Hence the use of 1.

If the list only contained the number “1”, our returned value would still be correct.

Let me know if that makes sense!

John Michalak about 10 years

ditto this is exactly how i did it… first i put 0 instead of 1 and it was returning 0 and looked back and duh… anything times 0 is 0 lol

Hubert_93 about 10 years

JAY_123 // You would know that ‘total’ should be integer, so that we allocate ‘total’ for ‘1’.

And the reason that integer is ‘1’, not ‘0’, is well explained by our friends!!

firestarwang over 9 years

It worked for me!

Answer 52e15be8631fe9cdbd003449

4 votes

Permalink

I also think it can be shorter ;)

product = lambda lst: reduce(lambda x,y:x*y,lst)
points
Submitted by banksh
about 10 years

Answer 52fe3d6b548c3511a50025ac

0 votes

Permalink

This works pretty well

def remove_duplicates(x):
x = list(x)
x = list(set(x))
return x
points
Submitted by Richard
about 10 years

1 comments

Richard about 10 years

it makes x a list, then turns the list into a listed set (grouping numbers to only show once). Then returns the list

Answer 5316f0579c4e9d7d110018f8

0 votes

Permalink

import operator
def multi(l):
    return reduce(operator.mul, l)
print multi([4, 5, 5])
points
Submitted by Simo
about 10 years

Answer 53b97bd452f86378360005a2

0 votes

Permalink

def product(li): pro=li[0] for i in range(1,len(li)): pro*=li[i] return pro print product(li) simple but i take more time to find the solution for this exercise

points
Submitted by Renjith S Raj
over 9 years

Answer 55466b9c937676d3c5000491

0 votes

Permalink

def product(lst):
    total = 1
    for num in lst:
        total *= num
    return total

Easiest method, and I assume this is the intended method for this tutorial.

points
Submitted by AndreasGaul
almost 9 years

Answer 52b30989282ae3c32a002190

-6 votes

Permalink

Hmm is this the same as 13/15?

Either way, this is the easiest way of calculating the product of a bunch of arguments:

from numpy import prod

def product(*args):
    return int(prod(*args))
points
Submitted by Martin Dahlin
over 10 years

3 comments

Anukul Sangwan over 10 years

Well, it might work; but this is not the way one is supposed to do it. Nowhere in the whole Codecademy Python course are we instructed to import product from numpy; so I guess this is not what is expected out of a beginner on Codecademy. Still, good that you know of it.

Gottlieb Uahengo about 10 years

I think it could be shorter…

Gottlieb Uahengo about 10 years

Well, if everything we did is just what was “expected,” we wouldn’t have progressed very far now. @Anukul