In programming, determining if a list is a palindrome is a classic exercise that can sharpen your algorithmic thinking and Python skills. A palindrome is a sequence that remains the same even when reversed. Given a List, check if it is 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 the first and reverse 2nd half of the 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)) |
The original list is : [1, 4, 5, 4, 1] Is list Palindrome : True
Time complexity: O(n)
Auxiliary space: O(n)
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)) |
The original list is : [1, 4, 5, 4, 1] Is list Palindrome : True
Time Complexity: O(n) where n is the number of elements in the list “test_list”. reversed() performs n number of operations.
Auxiliary Space: O(1), no extra space is required
Method 3 : Using reverse() method
One of the best approach is to reverse the list and then compare it to the original list. If they match, the list is a palindrome.
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" ) |
The original list is : [1, 4, 5, 4, 1] Is palindrome ? Yes
Approach 4: Using Iterative comparision
Testing if a list is a palindrome using an iterative approach involves comparing elements from both ends of the list towards the center. Iterate through the first half of the input list.Compare the elements at the current position with their corresponding elements from the end of the list.If any pair of elements doesn’t match, return No
as it’s not a palindrome.If all pairs match, return Yes
indicating that the list is a palindrome.
Python3
def is_palindrome(input_list): length = len (input_list) for i in range (length / / 2 ): if input_list[i] ! = input_list[length - i - 1 ]: return 'No' return 'yes' test_list1 = [ 4 , 5 , 4 ] test_list2 = [ 4 , 5 , 5 ] print (is_palindrome(test_list1)) print (is_palindrome(test_list2)) |
yes No
Time Complexity: O(n), where n is the length of the input list.
Space Complexity: O(1), which is constant.