Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to check whether number formed by combining all elements of the array is palindrome
In Python, an array (list) is a collection of comma-separated values enclosed in square brackets. Lists are flexible as their elements don't need to be of the same type.
In this article, we will learn how to check whether a number formed by combining all elements of an array is a palindrome ?
Methods Used
The following are the various methods to accomplish this task ?
Using map() & join() functions
Using type casting & string concatenation
Using str(), list(), extend(), join() and reverse() functions
Example Scenario
Consider an input list. We will check whether the number formed by combining all elements is a palindrome ?
Input
inputList = [3, 25, 42, 24, 52, 3]
Expected Output
Yes, it is a Palindrome
All input list elements are combined to form 3254224523. This number reads the same forwards and backwards, making it a palindrome.
Using map() & join() functions
The map() function applies a function to every element in a list. The join() function connects sequence elements to form a string ?
# creating a function to check whether the string passed to it
# is a palindrome or not
def checkingPalindrome(inputString):
# reversing the input string
reverseStr = inputString[::-1]
# checking whether the input string is equal to its reverse string
if(inputString == reverseStr):
return True
else:
return False
# creating a function to convert the input list into a string
def joiningList(inputList):
# converting all the input list elements into strings
inputList = list(map(str, inputList))
# converting the input list to a string using the join() function
resultNum = ''.join(inputList)
# checking whether the converted string number is a palindrome
if(checkingPalindrome(resultNum)):
return True
else:
return False
# input list
inputList = [3, 25, 42, 24, 52, 3]
# checking whether the combined number is a palindrome
if(joiningList(inputList)):
print("Yes, it is a Palindrome")
else:
print("No, it is NOT a Palindrome")
Yes, it is a Palindrome
Using Type Casting & String Concatenation
This approach manually converts each element to a string and concatenates them ?
# creating a function to check palindrome
def checkingPalindrome(inputString):
reverseStr = inputString[::-1]
if(inputString == reverseStr):
return True
else:
return False
# creating a function to convert the input list into a string
def joiningList(inputList):
# Creating an empty string for storing list elements
resultNum = ""
# traversing through the input list
for e in inputList:
# converting each element to string and concatenating
resultNum = resultNum + str(e)
# checking if the result is a palindrome
if(checkingPalindrome(resultNum)):
return True
else:
return False
# input list
inputList = [3, 25, 42, 24, 52, 3]
if(joiningList(inputList)):
print("Yes, it is a Palindrome")
else:
print("No, it is NOT a Palindrome")
Yes, it is a Palindrome
Using str(), list(), extend(), and reverse() functions
The extend() function adds elements from one list to another. The reverse() function reverses a list in place ?
# input list
inputList = [3, 25, 42, 24, 52, 3]
# empty string for storing combined elements
resultantStr = ""
# traversing through the input list
for e in inputList:
# converting each element to string and adding
resultantStr += str(e)
# converting the result string into a list of characters
newList = list(resultantStr)
# creating a copy using extend
k = []
k.extend(newList)
# reversing the original list
newList.reverse()
# checking if original and reversed are the same
if(newList == k):
print("Yes, it is a Palindrome")
else:
print("No, it is NOT a Palindrome")
Yes, it is a Palindrome
Comparison
| Method | Approach | Best For |
|---|---|---|
| map() & join() | Functional programming | Clean, readable code |
| String concatenation | Manual iteration | Understanding the process |
| extend() & reverse() | List manipulation | Working with character lists |
Conclusion
We explored three methods to check if a number formed by combining array elements is a palindrome. The map() and join() approach is most efficient, while string concatenation offers better understanding of the underlying process.
