Python – Test if list is Palindrome
Given a List, check if its palindrome.
Input : test_list = [4, 5, 4]
Output : True
Explanation : List is same from front and rear.Input : test_list = [4, 5, 5]
Output : True
Explanation : List is not same from front and rear.
Method #1: Using list slicing
In this, we extract first and reversed 2nd half of list, and then compare for equality, if found equal, then we conclude its palindrome.
Python3
# Python3 code to demonstrate working of # Test if list is Palindrome # Using list slicing # initializing list test_list = [ 1 , 4 , 5 , 4 , 1 ] # printing original list print ( "The original list is : " + str (test_list)) # Reversing the list reverse = test_list[:: - 1 ] # checking if palindrome res = test_list = = reverse # printing result print ( "Is list Palindrome : " + str (res)) |
Output
The original list is : [1, 4, 5, 4, 1] Is list Palindrome : True
Method #2 : Using reversed()
In this, we simply reverse the list and check if both original list and reversed list is similar.
Python3
# Python3 code to demonstrate working of # Test if list is Palindrome # Using reversed() # initializing list test_list = [ 1 , 4 , 5 , 4 , 1 ] # printing original list print ( "The original list is : " + str (test_list)) # reversing list rev_list = list ( reversed (test_list)) # checking for Palindrome res = rev_list = = test_list # printing result print ( "Is list Palindrome : " + str (res)) |
Output
The original list is : [1, 4, 5, 4, 1] Is list Palindrome : True
Method #3 : Using reverse() method
Python3
#Using lists and reverse() method def isPalindrome(x): y = [] y.extend(x) x.reverse() if (x = = y): return True return False # Driver Code test_list = [ 1 , 4 , 5 , 4 , 1 ] print ( "The original list is : " + str (test_list)) ans = isPalindrome(test_list) if ans: print ( "Is palindrome ? " + "Yes" ) else : print ( "Is palindrome ? " + "No" ) |
Output
The original list is : [1, 4, 5, 4, 1] Is palindrome ? Yes
Please Login to comment...