Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Check if two lists are reverse equal using Python?
When working with lists in Python, there may be cases where you need to compare whether two lists are reverse equal. This means that the elements in one list are the same as the elements in the other list but in reverse order. Python provides several methods to check if two lists are reverse equal: reversing and comparing lists, using the zip() function, and converting lists to strings.
Method 1: Reversing and Comparing Lists
The first method involves reversing one of the lists using slicing and then comparing it with the other list. If the reversed list equals the original list, the two lists are reverse equal.
Syntax
reversed_list1 = list1[::-1]
Here, a reversed version of list1 is created using the slicing syntax list1[::-1], which returns a new list with elements in reverse order.
Example
In the below example, list1 contains the elements [1, 2, 3, 4, 5], and list2 contains the elements [5, 4, 3, 2, 1]. The function reverses list1 and checks if it equals list2 ?
def are_lists_reverse_equal(list1, list2):
reversed_list1 = list1[::-1]
return reversed_list1 == list2
list1 = [1, 2, 3, 4, 5]
list2 = [5, 4, 3, 2, 1]
print(are_lists_reverse_equal(list1, list2))
# Test with non-reverse equal lists
list3 = [1, 2, 3]
list4 = [1, 2, 3]
print(are_lists_reverse_equal(list3, list4))
True False
Method 2: Using the zip() Function
This method uses the zip() function to iterate over elements of both lists simultaneously. We compare elements from the start of one list with elements from the end of the other list using reversed().
Syntax
all(x == y for x, y in zip(list1, reversed(list2)))
The zip() function pairs each element from list1 with the corresponding element from the reversed list2. The all() function checks if all pairs are equal.
Example
The zip() function pairs each element from list1 with the corresponding element from the reversed list2. Since all pairs are equal, the output is True ?
def are_lists_reverse_equal(list1, list2):
return all(x == y for x, y in zip(list1, reversed(list2)))
list1 = [1, 2, 3, 4, 5]
list2 = [5, 4, 3, 2, 1]
print(are_lists_reverse_equal(list1, list2))
# Test with different lengths
list3 = [1, 2, 3]
list4 = [3, 2, 1, 0]
print(are_lists_reverse_equal(list3, list4))
True False
Method 3: Converting Lists to Strings
This method converts lists to strings and compares the reversed strings. This approach works well for lists containing numbers or single characters.
Syntax
str_list1 = ''.join(map(str, list1)) str_list2 = ''.join(map(str, list2)) return str_list1 == str_list2[::-1]
The map() function converts each element to a string, and join() concatenates them. We then compare the reversed string of list2 with the string of list1.
Example
The string representations of elements in both lists are concatenated, and the reversed string of list2 is compared with list1 ?
def are_lists_reverse_equal(list1, list2):
str_list1 = ''.join(map(str, list1))
str_list2 = ''.join(map(str, list2))
return str_list1 == str_list2[::-1]
list1 = [1, 2, 3, 4, 5]
list2 = [5, 4, 3, 2, 1]
print(are_lists_reverse_equal(list1, list2))
# Test with strings
list3 = ['a', 'b', 'c']
list4 = ['c', 'b', 'a']
print(are_lists_reverse_equal(list3, list4))
True True
Comparison
| Method | Memory Usage | Best For | Limitations |
|---|---|---|---|
Slicing [::-1]
|
Creates copy | Simple, readable | Uses extra memory |
zip() + reversed()
|
Memory efficient | Large lists | Stops at shortest list |
| String conversion | Creates strings | Simple data types | May fail with complex objects |
Conclusion
Use slicing [::-1] for simple and readable code. Use zip() with reversed() for memory efficiency with large lists. The string conversion method works well for basic data types but may have limitations with complex objects.
