Python Program to Print all Perfect Squares from a List using List Comprehension and Math Module

Given a list, the task is to write a program to print all perfect squares from the given list using list comprehension and Math Module.

Using list comprehension and the math module, you’ll learn how to check whether the elements in a Python list entered by the user are perfect squares or not.

List comprehensions are a neat trick that allows us to create a new list depending on the values of an existing list in only one line, making the code look shorter and more concise because we aren’t committing to the problem with an entire loop.
Python’s math module is a highly valuable tool because it offers a large number of mathematical functions that we may utilize in our programs.

Examples:

Example1:

Input:

Given list =[19, 24, 25, 36, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]

Output:

The given list is = [19, 24, 25, 36, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]
The perfect squares numbers of the given list is = [25, 36, 81, 144, 900, 225, 4, 9, 1, 16, 49, 49, 25, 25]

Example2:

Input:

Given list =[37, 82, 81, 467, 839, 8383, 1000, 1900, 10000, 9, 48, 49, 64, 121, 56]

Output:

Enter some random List Elements separated by spaces = 37 82 81 467 839 8383 1000 1900 10000 9 48 49 64 121 56
The given list is = [37, 82, 81, 467, 839, 8383, 1000, 1900, 10000, 9, 48, 49, 64, 121, 56]
The perfect squares numbers of the given list is = [81, 10000, 9, 49, 64, 121]

Python Program to Print all Perfect Squares from a List using List Comprehension and Math Module

Below are the ways to print all perfect squares from the given list using list comprehension and Math Module.

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Method #1: Using List Comprehension and Math Modules (Static Input)

Approach:

Import the math module using the import statement.

Give the list as static input and store it in a variable.

Using List comprehension, floor(), and sqrt() functions to check whether the element value of the list is a perfect square or not. (An Element is said to be a perfect square if the floor value of the square root of the number is equal to the sqrt of the number)

Ex:

floor(sqrt(9))=3

sqrt(9)=3

Hence 9 is a perfect square.

Print the new list which contains only perfect squares of the original list.

The Exit of the program.

Below is the implementation:

# Import the math module using the import statement.
import math
# Give the list as static input and store it in a variable.
gvnlst = [19, 24, 25, 36, 81, 144, 600, 900,
          225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]
print('The given list is =', gvnlst)
# Using List comprehension, floor(), and sqrt() functions
# to check whether the element value of the list is a perfect square or not.
# (An Element is said to be a perfect square if the floor value of the square root
# of the number is equal to the sqrt of the number)
prftsquareslist = [elemen for elemen in gvnlst if (
    math.sqrt(elemen) == math.floor(math.sqrt(elemen)))]
# Print the new list which contains only perfect squares of the original list.
print('The perfect squares numbers of the given list is =', prftsquareslist)

Output:

The given list is = [19, 24, 25, 36, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]
The perfect squares numbers of the given list is = [25, 36, 81, 144, 900, 225, 4, 9, 1, 16, 49, 49, 25, 25]

Method #2: Using List Comprehension and Math Modules (User Input)

Approach:

Import the math module using the import statement.

Give the list as user input using list(),map(),input(),and split() functions.

Store it in a variable.

Using List comprehension, floor(), and sqrt() functions to check whether the element value of the list is a perfect square or not. (An Element is said to be a perfect square if the floor value of the square root of the number is equal to the sqrt of the number)

Ex:

floor(sqrt(9))=3

sqrt(9)=3

Hence 9 is a perfect square.

Print the new list which contains only perfect squares of the original list.

The Exit of the program.

Below is the implementation:

# Import the math module using the import statement.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlst = list(
    map(int, input('Enter some random List Elements separated by spaces = ').split()))
print('The given list is =', gvnlst)
# Using List comprehension, floor(), and sqrt() functions
# to check whether the element value of the list is a perfect square or not.
# (An Element is said to be a perfect square if the floor value of the square root
# of the number is equal to the sqrt of the number)
prftsquareslist = [elemen for elemen in gvnlst if (
    math.sqrt(elemen) == math.floor(math.sqrt(elemen)))]
# Print the new list which contains only perfect squares of the original list.
print('The perfect squares numbers of the given list is =', prftsquareslist)

Output:

Enter some random List Elements separated by spaces = 37 82 81 467 839 8383 1000 1900 10000 9 48 49 64 121 56
The given list is = [37, 82, 81, 467, 839, 8383, 1000, 1900, 10000, 9, 48, 49, 64, 121, 56]
The perfect squares numbers of the given list is = [81, 10000, 9, 49, 64, 121]

Related Programs: