Check if a number in a list is perfect square using Python


In this article, we will learn a Python program to check if a number is a Perfect Square in a List.

What is a Perfect Square?

A perfect square is an integer that can be written as the square of another integer. It is referred to as the product of some integer with itself in other words.

Example

25 = 5x5

Methods Used

The following are the various methods to accomplish this task −

  • Using List comprehension & math module

  • Using For Loop & math module

  • Using ** Operator

  • Using the filter() and lambda functions

The sqrt() and floor() functions are the two most important ones in the math module that we'll be using here to find all the perfect squares in an input list.

Method 1: Using List Comprehension & Math Module

list comprehension() − When you want to create a new list based on the values of an existing list, list comprehension provides a concise syntax.

The sqrt() function calculates the square root of any number passed to it as an argument.

The floor() function − rounds any decimal number down to its closest integer.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Use the import keyword to import the math module.

  • We are importing this math module for using the sqrt() and floor() functions.

  • Create a variable to store the input list and print it.

  • Use the list comprehension method, to find the perfect squares in an input list by checking whether the square root of a number is equal to the floor of the square root of the given number.

  • Print the list of all the perfect squares in an input list.

Example

The following program returns all the perfect squares in an input list using the math module and List comprehension −

# importing math module
import math

# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]

# Printing the input list
print("Input List: ", inputList)

# list comprehension by checking whether the square root
# of number is equal to the floor of 
perfectSquares = [k for k in inputList if (
   math.sqrt(k) == math.floor(math.sqrt(k)))]

# Printing all the perfect squares in an input list
print("Perfect squares in an input list: ", perfectSquares)

Output

On executing, the above program will generate the following output −

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  [4, 16, 36, 25, 81, 49, 100]

Method 2: Using For Loop & Math Module

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Create an empty list for storing the resultant perfect squares in a list.

  • Use the for loop, to traverse through each element of the input list.

  • Use the if conditional statement to determine whether the corresponding list element is a perfect square or not by checking if the square of a list element is equal to the floor of the square root of the given number.

  • Use the append() function(adds the element to the list at the end) to append an element to the result list if the condition is true.

Example

The following program returns all the perfect squares in an input list using the math module and for loop −

# importing math module
import math
# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
# Printing the input list
print("Input List: ", inputList)
# creating an empty list for storing the resultant perfect squares
perfectSquares = [] 

# traversing through each element of the input list 
for k in inputList:
   # checking whether the corresponding list element is a perfect square 
   if (math.sqrt(k) == math.floor(math.sqrt(k))):
      # appending an element to the result list if the condition is true
      perfectSquares.append(k)
    
print("Perfect squares in an input list: ", perfectSquares)

Output

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  [4, 16, 36, 25, 81, 49, 100]

Method 3: Using ** Operator

Example

The following program returns all the perfect squares in an input list using the math module and ** operator −

# importing math module
import math
# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
print("Input List: ", inputList)
# creating an empty list for storing the resultant perfect squares
perfectSquares = [] 

# traversing through each element of the input list 
for k in inputList:
   # checking whether the corresponding list element is a perfect square
   if (k**0.5 == math.floor(k**0.5)):
      # appending element to the result list if the condition is true
      perfectSquares.append(k)      

print("Perfect squares in an input list: ", perfectSquares)

Output

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  [4, 16, 36, 25, 81, 49, 100]

Method 4: Using the filter() and Lambda Functions

Example

The following program returns all the perfect squares in an input list using the filter() and lambda functions −

# importing math module
import math
# input list
inputList = [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
print("Input List: ", inputList)

# Filtering all perfect square list elements using lambda functions
perfectSquares =filter(lambda k: math.sqrt(k) == math.floor(math.sqrt(k)), inputList)     
# converting to list        
perfectSquares = list(perfectSquares)

print("Perfect squares in an input list: ", *perfectSquares)

Output

Input List:  [10, 4, 5, 35, 16, 80, 36, 25, 40, 81, 49, 90, 200, 100]
Perfect squares in an input list:  4 16 36 25 81 49 100

Conclusion

We covered how to get all perfect squares elements in a list using four approaches in this article. Using the filter() and lambda functions, we learnt how to apply a filter on list elements. We also learnt how to get the square root or power of a number by utilizing the ** operator rather than importing the sqrt() or pow() methods.

Updated on: 23-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements