I\'m writing a function that takes a list L as a parameter and returns a list consisting of all the elements in L that are perfect squares.
def isPerfectSquare(
You have to import math
in isPerfectSquare
, otherwise it is just imported in the local scope of the perfetSquares2
function.
However, PEP 8 suggests you put module imports at the top of scripts:
import math
def isPerfectSquare(n):
return n==int(math.sqrt(n))**2
def perfectSquares2(L):
return(list(filter(isPerfectSquare,(L))))
By the way, I think a list comprehension may be faster here:
def perfectSquares2(L):
return [i for i in L if isPerfectSquare(i)]