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. In Python, we can check if two lists are reverse equal using methods like Reversing and Comparing Lists, using the zip() Function, converting Lists to Strings, etc. In this article, we will understand these methods and check if two lists are reverse equal with the help of various examples.

Method 1:Reversing and Comparing Lists

The first method involves reversing one of the lists and then comparing it with the other list. If the reversed list is equal to the original list, we can say that 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 are_lists_reverse_equal is called with these lists as arguments. The function reverses list1 and checks if it is equal to list2. Since the reversed list is equal to list2, the output is True.

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))

Output

True

Method 2:Using the zip() Function

This method involves using the zip() function to iterate over the elements of both lists simultaneously. We compare the elements from the start of one list with the elements from the end of the other list. If all the corresponding elements are equal, the two lists are reverse equal.

Syntax

all(x == y for x, y in zip(list1, reversed(list2)))

Here, the zip() function iterates over the elements of both lists simultaneously. Inside the zip() function, we pair each element from list1 with the corresponding element from the reversed version of list2 using reversed(list2). The all() function is used to check if all the pairs of elements are equal.

Example

In the below example, list1 and list2 are the same as in the previous example. The function are_lists_reverse_equal is called with these lists as arguments. The zip() function pairs each element from list1 with the corresponding element from the reversed list2. Since all the 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))

Output

True

Method 3:Converting Lists to Strings

This method involves converting the lists to strings and comparing the reversed strings. If the reversed string of one list is equal to the string representation of the other list, the two lists are reverse equal.

Syntax

str_list1 = ''.join(map(str, list1))
str_list2 = ''.join(map(str, list2))
 return str_list1 == str_list2[::-1]

Here, the map() function with str as the first argument is used to convert each element of list1 and list2 to a string. The join() method is used to concatenate the string representations of the elements into a single string for both lists. We then compare the reversed string of list2, str_list2[::-1], with str_list1. If they are equal, the function returns True; otherwise, it returns False.

Example

In the below example, list1 and list2 remain the same. The function are_lists_reverse_equal is called with these lists as arguments. The string representations of the elements in both lists are concatenated, and the reversed string of list2 is compared with list1. Since they are equal, the output is True.

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))

Output

True

Conclusion

In this article, we discussed how we can check if two lists are reverse equal using different ways in Python. We explored how to reverse and compare lists, utilize the zip() function for comparison, and convert lists to strings for comparison. Each method is simple and straightforward and can be used anytime depending on the demand of the problem in hand.

Updated on: 17-Jul-2023

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements