Square root list in python
Question:
I am trying to minimize as much as possible my coding and trying to solve problems in different ways and i wrote this code which gets a list of numbers and it returns the square root of each one number in list
def square_roots(l):
squareRoots = []
result = [squareRoots.append(math.sqrt(i)) for i in l]
return result
l=[1,2,3]
print(square_roots(l))
The only problem is that it returns [None, None, None]
instead of the square root of each number in the array.
What’s wrong with it?
Answers:
list.append()
(i.e. squareRoots.append()
) returns None
. Remove that part.
def square_roots(l):
result = [math.sqrt(i) for i in l]
return result
You might want to read Why does append() always return None in Python?
I am trying to minimize as much as possible my coding and trying to solve problems in different ways and i wrote this code which gets a list of numbers and it returns the square root of each one number in list
def square_roots(l):
squareRoots = []
result = [squareRoots.append(math.sqrt(i)) for i in l]
return result
l=[1,2,3]
print(square_roots(l))
The only problem is that it returns [None, None, None]
instead of the square root of each number in the array.
What’s wrong with it?
list.append()
(i.e. squareRoots.append()
) returns None
. Remove that part.
def square_roots(l):
result = [math.sqrt(i) for i in l]
return result
You might want to read Why does append() always return None in Python?