Home »
Python »
Python Programs
Python | Program to print Palindrome numbers from the given list
In this, we have a list of numbers from that list we have to print only palindrome numbers present in that list, palindrome numbers are numbers, on reversing which number remains the same.
By Anamika Gupta Last updated : August 24, 2023
Palindrome Numbers
Palindrome numbers are those numbers that are equal to their reverse value I.e., the numbers that remain the same when their digits are reversed. The Palindrome numbers are also known as numeral palindrome numbers). For example, 12321 is a Palindrome number, because when we reverse its digits (12321) it is the same as the initial number. First few palindrome numbers are 0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 101 , 111 , 121 , ... and so on.
Problem Statement
Given a number n, size of list then next line contains space separated n numbers.
Logic
We will simply convert the number into string and then using reversed(string) predefined function in python ,we will check whether the reversed string is same as the number or not.
Algorithm/Steps
The following are the algorithm/steps to print Palindrome numbers from the given Python list:
- Input list elements (Here, we are separating them with spaces) using int() and input() methods.
- Convert this input into a list with the help of list() and map() methods.
- Run a for loop to iterate the list elements.
- Convert each element to a string using str() method.
- Check the number (converted to string) is equal to its reverse.
- If the number is equal to its reverse, print it.
Python program to print Palindrome numbers from the given list
# Give size of list
n = int(input("Enter total number of elements: "))
# Give list of numbers having size n
l = list(map(int, input().strip().split(" ")))
# Print the input list
print("Input list elements are:", l)
# Check through the list to check
# number is palindrome or not
print("Palindrome numbers are:")
for i in l:
num = str(i)
if "".join(reversed(num)) == num:
print(i)
Output
The output of the above program is:
RUN 1:
Enter total number of elements: 5
10 20 21 33 676
Input list elements are: [10, 20, 21, 33, 676]
Palindrome numbers are:
33
676
RUN 2:
Enter total number of elements: 5
121 343 22 12 10
Input list elements are: [121, 343, 22, 12, 10]
Palindrome numbers are:
121
343
22
Python Basic Programs »