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?

Asked By: jimmys

||

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?

Answered By: wjandrea
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.