How to pass a list as an input of a function in Python
Solution 1
You are currently returning a value from your function in the first iteration of your for
loop. Because of this, the second and third iteration of your for
loop never take place. You need to move your return
statement outside of the loop as follows:
import math
def square(x):
result = []
for y in x:
result.append(math.pow(y,2.0))
return result
print(square([1,2,3]))
Output
[1.0, 4.0, 9.0]
Solution 2
Why not side-step the problem altogether?
def square(vals):
return [v*v for v in vals]
Edit: The first problem, as several people have pointed out, is that you are short-circuiting your for
loop. Your return
should come after the loop, not in it.
The next problem is your use of list.append
- you need to call it, not assign to it, ie result.append(y*y)
. result.append = y*y
instead overwrites the method with a numeric value, probably throwing an error the next time you try to call it.
Once you fix that, you will find another less obvious error occurs if you call your function repeatedly:
print(square([1,2,3]) # => [1, 4, 9]
print(square([1,2,3]) # => [1, 4, 9, 1, 4, 9]
Because you pass a mutable item (a list) as a default, all further use of that default item points back to the same original list.
Instead, try
def square(vals, result=None):
if result is None:
result = []
result.extend(v*v for v in vals)
return result
Solution 3
We even use result
? You can use a list comprehension to generate your result which you then return. I'm not sure why you passed result
as a variable into the function, since it is not used.
Also, having return result
inside your loop means the function returns the value on the first iteration, so it just returns the square of the first number in the list.
import math
def square(x):
return [math.pow(y, 2) for y in x]
>>> print(square([1,2,3]))
[1.0, 4.0, 9.0]
Solution 4
You might be interested in using yield
def square(x):
for y in x:
yield math.pow(y, 2.0)
that way you can either call
for sq in square(x):
...
which won't generate the entire list of squares at once but rather one element per iteration, or use list(square(x))
to obtain the full list on demand.
Solution 5
You should return outside the for loop. Otherwise, it will stop after first iteration.
def square(x):
result=[]
for y in x:
result.append(math.pow(y,2.0)) # add to list after calculation
return result
print(square([1,2,3])
Arpan Das
Updated on December 01, 2020Comments
-
Arpan Das over 3 years
I am using Python, and I have a function which takes a list as the argument. For example, I am using the following syntax,
def square(x,result= []): for y in x: result.append=math.pow(y,2.0) return result print(square([1,2,3]))
and the output is
[1]
only where I am supposed to get[1,4,9]
.What am I doing wrong?
-
ShaneQful over 8 yearsThis doesn't really tell the OP where they are going wrong and is just a one liner of how to square an array
-
BasedRebel over 8 years@ShaneQful: please take another look.
-
muru over 8 yearsWell, it could be seen as the initial value, for example. Perhaps OP intended
square([1,2,3],[100,200,300])
to return[100,200,300,1,4,9]
. -
Pharap over 8 yearsAs someone used to brace based programming rather than indentation based, I must say that single tab changing where the function returns from is something I find horribly unintuitive.
-
Matthias over 8 yearsBut you still indent your code even if your using braces, don't you?
-
Cees Timmerman over 8 yearsAppend is a function you don't want to overwrite here.
-
ShaneQful about 8 yearsApologies, my initial comment above was based on your initial succinct answer. Have an up vote :)
-
Christoph Rackwitz about 2 yearsthis is the only answer that even mentions the mistaken assignment to result.append, which is an error in all situations.