
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Number Formed by Array is Palindrome in Python
In python an array(list) of comma-separated values (items) enclosed in square brackets is one of the most flexible data types available in Python. The fact that a list's elements do not have to be of the same type is important.
In this article, we will learn a python program to check whether a number formed by combining all elements of the 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
Assume we have taken an input list. We will now check whether the number formed by combining all elements of the list is a palindrome or not using the above methods.
Input
inputList = [3, 25, 42, 24, 52, 3]
Output
Yes, it is a Palindrome
In the above example, all the input list elements are combined to form a single number in string form i.e, 3254224523. This number is a palindrome. Hence the output is a palindrome
Using map() & join() functions
str() function(returns the string format of the object i.e converts into string)
join() ? join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.
list() function(converts the sequence/iterable to a list).
map() function - maps any function over an iterator/list.
Example
The following program checks whether a number formed by combining all elements of the array is a palindrome using map() and join() functions -
# 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): # returning True if the condition is true return True else: # else returning False return False # creating a function to convert the input list into a string # by accepting the input list as an argument to it. def joiningList(inputList): # converting all the input list elements into a string inputList = list(map(str, inputList)) # converting the input list to a string using the join() function resultNum = ''.join(inputList) # checking whether the above-converted string number is a palindrome # by calling the above defined checkingPalindrome() function if(checkingPalindrome(resultNum)): # returning True if the function returns True return True else: # else returning False return False # main code # input list inputList = [3, 25, 42, 24, 52, 3] # Calling the above defined joiningList() function by passing input list to it # and checking whether the function returns True/False if(joiningList(inputList)): # printing Palindrome if the joiningList() fuction returns True print("Yes, it is a Palindrome") else: # else printing NOT a Palindrome print("No, it is NOT a Palindrome")
Output
On executing, the above program will generate the following output -
Yes, it is a Palindrome
Using type casting & string concatenation
The following program checks whether a number formed by combining all elements of the array is a palindrome using type casting & string concatenation -
Example
# creating a function to check whether the string passed to is # 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): # returning True if the condition is true return True else: # else returning False return False # creating a function to convert the input list into a string # by accepting the input list as an argument to it. def joiningList(inputList): # Creating an empty string for storing list elements as a number resultNum = "" # travsering through the input list for e in inputList: # converting each element of list into a string e = str(e) # adding the string element to the resultNum resultNum = resultNum + e # checking whether the above-converted string number is a palindrome # by calling the above defined checkingPalindrome() function if(checkingPalindrome(resultNum)): # returning True if the function returns True return True else: # else returning False return False # input list inputList = [3, 25, 42, 24, 52, 3] # Calling the above defined joiningList() function by passing input list to it # and checking whether the function returns True/False if(joiningList(inputList)): # printing Palindrome if the joiningList() fuction returns True print("Yes, it is a Palindrome") else: # else printing NOT a Palindrome print("No, it is NOT a Palindrome")
Output
On executing, the above program will generate the following output -
Yes, it is a Palindrome
Using str(), list(), extend(), join() and reverse() functions
extend() function(adds all the elements of an iterable like list, tuple, string etc to the end of the list)
reverse() function ? a built-in function that is used to reverse objects of the list. It simply alters the original list rather than taking up additional space.
The following program checks whether a number formed by combining all elements of the array is a palindrome using type casting & string concatenation -
Example
# input list inputList = [3, 25, 42, 24, 52, 3] # an empty string for storing list elements as a number resultantStr = "" # travsering through the input list for e in inputList: # converting each element of the list as a string and adding it to the result string resultantStr += str(e) # converting the result string into a list newList = list(resultantStr) # empty list k = [] # extending the new list to the above empty list k.extend(newList) # reversing the new list newList.reverse() # checking whether the new list is equal to the extended list if(newList == k): # printing Palindrome if the condition is True print("Yes, it is a Palindrome") else: # Else printing NOT a Palindrome print("No, it is NOT a Palindrome")
Output
On executing, the above program will generate the following output -
Yes, it is a Palindrome
Conclusion
In this article we have learned how to check whether number formed by combining all elements of the array is palindrome using 3 different approaches. We also learned how to use the map() method to apply a function to every element in the list. Using the extend() function, we finally learned how to combine two lists.