Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python Program to Check if a Number is Perfect Square in List

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will understand how to print all perfect squares from a list in Python using the list comprehension and math module. The main function that is present in the math module that we will be using is the sqrt()  and floor() function. Let’s see the output of these two functions one by one.

Square Root function(sqrt)

 As the name itself suggests, this function is used to calculate the square root of any number.

print(math.sqrt(100)) #Output will be 10

Floor function(floor)

The floor function is an interesting and handy function as it rounds any decimal number down to its closest integer. It is present in the math module as well which we discussed earlier. 

print(math.floor(5.3)) #Output will be 5

Now after learning all the prerequisites, let us now move on to the program that will print only perfect squares from a list.

Example 1:

Python3




import math
 
# Creating A List
Numbers = [4, 16, 17, 11, 36, 82,
           81, 49, 110, 120, 100]
 
# Printing the original array
print("The original List is : ", Numbers)
 
# Using List comprehension to find perfect squares
perfect_squares = [i for i in Numbers if (
    math.sqrt(i) == math.floor(math.sqrt(i)))]
 
# Printing the perfect squares
print("The perfect squares are: ", perfect_squares)


Output

The original List is :  [4, 16, 17, 11, 36, 82, 81, 49, 110, 120, 100]
The perfect squares are:  [4, 16, 36, 81, 49, 100]

Time Complexity: O(n) 
Auxiliary Space: O(1)

Understanding the Code:

  1. import math: Used to import the math module needed to execute functions like sqrt() and floor().
  2. perfect_squares: This is a list that we have created in order to store the perfect squares from the original list. What it does is, it takes the square root of every element present in the original list, and if the answer is an integer then it is a perfect square, and only then it is added to the perfect squares list. If the square root gives us a decimal number, then it is not a perfect square and we don’t add that number to our perfect_squares list.

Example 2:

Python3




import math
 
# Creating a list
 
Nums = [500, 540, 11, 10, 8, 4, 144, 256, 343, 121]
 
# Printing the original array
print("The original list is: ", Nums)
 
# Using List comprehension to find perfect squares
p_square = [i for i in Nums if (math.sqrt(i) == math.floor(math.sqrt(i)))]
# Printing the perfect squares
print("Perfect squares: ", p_square)


Output

The original list is:  [500, 540, 11, 10, 8, 4, 144, 256, 343, 121]
Perfect squares:  [4, 144, 256, 121]

The time complexity of this code is O(n), because the code is iterating over the input list “Nums” with a length of n, and checking the condition math.sqrt(i) == math.floor(math.sqrt(i)) for each element in the list, which takes O(1) time. Hence the total time complexity is O(n).

The Auxiliary space of this code is O(n), because the code creates a new list “p_square” to store the perfect squares, which takes a space of n at most.


My Personal Notes arrow_drop_up
Last Updated : 14 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials