While programming in python, comparison has to be done very often for checking different conditions. We may need to compare two variables or two groups of variables for a single condition checking. In this article we will try different ways to compare two lists in python. While comparing, we will have to check if both the lists contain the same elements or not irrespective of the order in which the elements are present in the lists. Accordingly, we will have to print the result.
Compare two lists using sort() method
To check whether two lists contain the same elements or not, we can use the sort() method to sort the elements of the lists first. Then, we can compare the two lists.
For comparison,first we will check if the length of the lists are equal or not. If the lengths are not equal, the lists will be automatically considered as different.
If the length of the lists are the same, we will sort the two lists. Then we will compare the lists using the == operator to check whether the lists are equal or not. If the sorted lists are equal, it will establish that both the original lists contain the same elements. This can be implemented as follows.
# function to compare lists
def compare(l1, l2):
# here l1 and l2 must be lists
if len(l1) != len(l2):
return False
l1.sort()
l2.sort()
if l1 == l2:
return True
else:
return False
list1 = [1, 2, 3, 4]
list2 = [1, 4, 3, 2]
list3 = [2, 3, 4, 5]
print("list1 is:",list1)
print("list2 is:",list2)
print("list3 is:",list3)
# comparing list1 and list 2
print("list1 and list2 contain same elements:",compare(list1, list2))
#comparing list2 and list3
print("list1 and list3 contain same elements:",compare(list1, list3))
Output:
list1 is: [1, 2, 3, 4]
list2 is: [1, 4, 3, 2]
list3 is: [2, 3, 4, 5]
list1 and list2 contain same elements: True
list1 and list3 contain same elements: False
Compare using sets in Python
To compare two lists in python, we can use sets. A set in python only allows unique values in it. We can use this property of sets to find if two lists have the same elements or not.
For comparison,first we will check if the length of the lists are equal or not. If the lengths are not equal, the lists will be automatically flagged as different.
After that, we will convert the lists into sets using set() constructor. We can compare the two sets using the == operator to check if both the sets are equal or not. If both sets are equal, it will be established that both the lists contain equal values. Following example illustrates this concept.
# function to compare lists
def compare(l1, l2):
# here l1 and l2 must be lists
if len(l1) != len(l2):
return False
set1 = set(l1)
set2 = set(l2)
if set1 == set2:
return True
else:
return False
list1 = [1, 2, 3, 4]
list2 = [1, 4, 3, 2]
list3 = [2, 3, 4, 5]
print("list1 is:", list1)
print("list2 is:", list2)
print("list3 is:", list3)
# comparing list1 and list 2
print("list1 and list2 contain same elements:", compare(list1, list2))
# comparing list2 and list3
print("list1 and list3 contain same elements:", compare(list1, list3))
Output:
list1 is: [1, 2, 3, 4]
list2 is: [1, 4, 3, 2]
list3 is: [2, 3, 4, 5]
list1 and list2 contain same elements: True
list1 and list3 contain same elements: False
Compare two lists using frequency counter
We can also compare two lists without comparing their lengths. For this, first we will have to create a python dictionary for each list which will keep track of the frequency of the elements in the lists. After creating the dictionaries where elements of the lists are stored as keys and their frequency are stored as values, we can compare the frequency of each element in the two dictionaries. If the frequency of each elements becomes equal, it will be confirmed that both the lists contained equal elements.
For this task, we can use the counter() method. The counter() method, when invoked on a list, creates a python dictionary and stores the elements as keys and their frequency as values in it. After invoking the counter() method, we can compare the created dictionary using == operator to check whether the frequency of every element is equal or not. If the result is True, the lists contain equal elements. Otherwise not. This can be seen from the following example.
import collections
# function to compare lists
def compare(l1, l2):
# here l1 and l2 must be lists
if len(l1) != len(l2):
return False
counter1 = collections.Counter(l1)
counter2=collections.Counter(l2)
if counter1 == counter2:
return True
else:
return False
list1 = [1, 2, 3, 4]
list2 = [1, 4, 3, 2]
list3 = [2, 3, 4, 5]
print("list1 is:", list1)
print("list2 is:", list2)
print("list3 is:", list3)
# comparing list1 and list 2
print("list1 and list2 contain same elements:", compare(list1, list2))
# comparing list2 and list3
print("list1 and list3 contain same elements:", compare(list1, list3))
Output:
list1 is: [1, 2, 3, 4]
list2 is: [1, 4, 3, 2]
list3 is: [2, 3, 4, 5]
list1 and list2 contain same elements: True
list1 and list3 contain same elements: False
Conclusion
In this article, we have seen three different ways to compare two lists in python and have checked if they contain the same elements without considering the position of the elements. To read more about lists, read this article on list comprehension.
In the compare() function used in the examples, it may be possible that user passes two other objects instead of lists. In such cases, the program may run into error. To avoid this, we can use exception handling using python try except to avoid runtime errors by applying type checking using type() method in try-except block to check if the objects passed as arguments are lists.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.