- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Test if list is Palindrome
When it is required to test if a list is a palindrome, a method is defined that reverses the string and checks if it is equal to the original string. Based on the result, relevant message is displayed on the console. A list comprehension and the ‘join’ method are used.
Example
Below is a demonstration of the same
def check_palindrome_list(my_str): if my_str == my_str[::-1]: print("The list is a palindrome") else: print("The list isn't a palindrome") my_list = [77, 1, 56, 65, 1, 77] print("The list is :") print(my_list) my_list = ' '.join([str(elem) for elem in my_list]) check_palindrome_list(my_list)
Output
The list is : [77, 1, 56, 65, 1, 77] The list is a palindrome
Explanation
A method named ‘check_palindrome_list’ is defined that takes a string as a parameter.
The string is reversed and it is compared to the original string.
Based on the result, relevant message is displayed on the console.
Outside the method, a list is defined and is displayed on the console.
It is iterated over and the ‘join’ method is used to join the elements and converted to a string.
The method is called by passing the required parameter.
The output is displayed on the console.
- Related Articles
- Palindrome Linked List in Python
- Python – Test if tuple list has a single element
- How to test if a List is an Unmodifable List in Java?
- Python – Test if elements of list are in Min/Max range from other list
- Python program to check if binary representation is palindrome?
- Test if tuple is distinct in Python
- Python program to test if all y occur after x in List
- Python Program to Check whether a Singly Linked List is a Palindrome
- Python program to check if a given string is number Palindrome
- Python program to check if a string is palindrome or not
- Python program to check if the given string is vowel Palindrome
- Check if binary representation of a number is palindrome in Python
- Check if number is palindrome or not in Octal in Python
- Check if a doubly linked list of characters is palindrome or not in C++
- Palindrome in Python: How to check a number is palindrome?
