In Python, a list is a data type that stores several elements as one datatype. List components can be changed, are not unique, and are ordered. We can use these list characteristics to execute different actions on a list.
If a list is sorted, comparing the matching elements of the two lists will allow us to quickly determine whether or not they contain identical elements in both.
However, what happens when we must determine whether two lists have the same components independent of the order in which the elements appear? For instance, the entries in the lists [1,2,3] and [2,3,1] are identical but arranged differently. These two lists cannot be compared directly since they are not sorted. What, then, do we do?
We will discuss the several approaches to this problem in this post, along with the benefits and drawbacks of each technique.
Method 1: Sorting and Comparing
This strategy is pretty uncomplicated. We run the two lists via a function that uses the’sorted()’ method to first sort the lists. The “sorted()” function returns two new lists that are sorted in ascending order from our original lists.
We can use the equal-to (‘==’) operator to compare the two sorted lists. We can infer that the original lists have the same elements but at different locations, if the sorted lists have the same entries in the same indices.
To learn about operators in Python, check out this article.
def check_lists(list1, list2): list1_sorted = sorted(list1) list2_sorted = sorted(list2) if list1_sorted == list2_sorted: return True else: return False list1 = [1, 2, 3, 4, 5] list2 = [4, 2, 5, 1, 3] if check_lists(list1, list2): print("The lists have the same elements") else: print("The lists do not have the same elements")
The lists have the same elements
In the code above, the ‘def_check()’ method compares the two lists, list1, and list2. As function parameters, the two lists are supplied. We obtain “list1_sorted” and “list2_sorted,” which are sorted copies of “list1” and “list2,” respectively, after calling the “sorted()” function on these two lists.
Then, using the “==” operator, we compare “list1_sorted” with “list2_sorted” element by element. The function returns a value of “True” if the elements of “list1_sorted” match their equivalent elements in “list2_sorted.” If that is not the case, it returns “False,” nonetheless.
Another drawback of using this method is that it is only successfully implemented if the size of both lists is equal. For example, there are two lists [1,2,3] and [1,1,2,3]. Both lists contain the same elements, but since the second list is larger due to duplicate elements, the ‘==’ operator won’t consider the two lists equal. What do we do in such a case?
Method 2: Using Sets
The above-described problem can easily be tackled by using sets. A set, in simple words, is an unordered collection of unique elements. Since a set has unique elements, that means there are no duplicates. Sets are also unordered, meaning elements within a set do not have a specific order or sequence.
Using these properties of sets, we can find out if two lists have the same elements regardless of their order and the size of the lists.
First, we convert the two lists into sets with the help of the ‘set()’ function. What this does is remove any duplicate elements from the lists. Also, as sets are unordered, the sequence of the elements is ignored.
Next, we simply compare the two sets by using the equal-to operator. This is to check if both sets have the same elements.
def check_sets(list1, list2): set1 = set(list1) set2 = set(list2) if set1 == set2: return True else: return False list1 = [1, 2, 3, 4, 5] list2 = [4, 2, 5, 1, 3, 4] if check_sets(list1, list2): print("The lists have the same elements") else: print("The lists do not have the same elements")
The lists have the same elements
In the above code, we have two lists, ‘list1’ and ‘list2’. The lists are passed as parameters to the function ‘check_sets().’ The function first converts these lists into sets using the ‘set()’’ function. We assign the outputs from the ‘set()’ function to ‘set1’ and ‘set2’. Then, we compare the two sets. If they both have the same elements, we get ‘True’ otherwise, ‘False.’
The time complexity of this method is O(n) on average case, where n is the length of the lists.
One important thing to keep in mind is that both of these methods assume that the list elements are comparable and hashable (an object is hashable if it has a hash value that remains constant), which is mostly the case for basic data types such as integers, floats, and strings.
There are two main methods to determine whether two Python lists contain the same elements in any order: sorting and comparing or using sets. Sorting and comparing work well for small lists without duplicates but not for those with duplicates. Sets are more versatile because they handle duplicates and ignore element order, making them ideal for larger lists. Choose the appropriate method based on the list’s size and nature to ensure efficient comparison. Additionally, these methods assume that the list elements are comparable and hashable, which is generally true for basic data types such as integers, floats, and strings. You can confidently tackle various list comparison scenarios in Python programming with a solid understanding of these methods.