﻿ Python List - Check if a list is a palindrome or not

# Python List Advanced Exercise - Check if a list is a palindrome or not

## Python List Advanced: Exercise-6 with Solution

Write a Python function to check if a list is a palindrome or not. Return true otherwise false.

Following solution uses the slicing method [::-1] to reverse the list. If the reversed list is equal to the original list, then the list is a palindrome.

Sample Solution:

Python Code:

``````# Define a function to check if a list is a palindrome (reads the same forwards and backwards)
def is_palindrome_list(lst):
return lst == lst[::-1]

# Create a list of numbers
nums = [1, 2, 4, 3, 5, 4, 6, 9, 2, 1]

# Print the original list
print("Original list:")
print(nums)

# Print a message to check if the list is a palindrome or not
print("Check the said list is Palindrome or not?")

# Call the is_palindrome_list function and print the result
print(is_palindrome_list(nums))

# Create a second list of numbers that is a palindrome
nums = [1, 2, 2, 1]

# Print the original list
print("\nOriginal list:")
print(nums)

# Print a message to check if the list is a palindrome or not
print("Check the said list is Palindrome or not?")

# Call the is_palindrome_list function with the second list and print the result
print(is_palindrome_list(nums))

# Create a list of colors
colors = ["Red", "Green", "Blue"]

# Print the original list
print("\nOriginal list:")
print(colors)

# Print a message to check if the list is a palindrome or not
print("Check the said list is Palindrome or not?")

# Call the is_palindrome_list function with the colors list and print the result
print(is_palindrome_list(colors))

# Create another list of colors that is not a palindrome
colors = ["Red", "Green", "Red"]

# Print the original list
print("\nOriginal list:")
print(colors)

# Print a message to check if the list is a palindrome or not
print("Check the said list is Palindrome or not?")

# Call the is_palindrome_list function with the second colors list and print the result
print(is_palindrome_list(colors))
```
```

Sample Output:

```Original list:
[1, 2, 4, 3, 5, 4, 6, 9, 2, 1]
Check the said list is Palindrome or not?
False

Original list:
[1, 2, 2, 1]
Check the said list is Palindrome or not?
True

Original list:
['Red', 'Green', 'Blue']
Check the said list is Palindrome or not?
False

Original list:
['Red', 'Green', 'Red']
Check the said list is Palindrome or not?
True
```

Flowchart:

What is the time complexity and space complexity of the following Python code?

``````def is_palindrome_list(lst):
return lst == lst[::-1]
``````

Time complexity - The time complexity of this code is O(n), where n is the length of the input list "lst". The lst[::-1] syntax creates a reversed copy of the input list "lst", which takes O(n) time since every element in the list needs to be copied.

Space complexity - The space complexity of this code is also O(n), since the reversed copy of the input list needs to be stored in memory. In addition, the original input list "lst" is also stored in memory. Since the input list is not modified, the space complexity remains O(n) regardless of the size of the input.

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿