Check if Two Lists Have Same Elements regardless of Order - Python Last Updated : 03 Dec, 2024 Comments Improve Suggest changes Like Article Like Report We have multiple ways to check if two lists have the same elements, regardless of their order. The simplest way to check if two lists have the same elements is to sort both lists and then compare them. If both lists have the same elements in the same order after sorting, they are the same. Python def check_lists(a, b): # Sort both lists a.sort() b.sort() # Compare if sorted lists are the same return a == b a = [3, 1, 2] b = [2, 1, 3] print(check_lists(a, b)) OutputTrue Using collections.CounterA more efficient method is to use the Counter class from Python's collections module. It allows us to count the number of occurrences of each element in the lists. If the counts match, the lists are the same. Python from collections import Counter def check_lists(a, b): # Use Counter to count elements in both lists return Counter(a) == Counter(b) a = [3, 1, 2] b = [2, 1, 3] print(check_lists(a, b)) OutputTrue Using a DictionaryWe can also use a dictionary to manually count the elements. This method is a bit more complex than the previous ones but still works well. Python def check_lists(a, b): # If the lengths of the lists are different, they cannot have the same elements if len(a) != len(b): return False # Create a dictionary to count occurrences of elements in list a count_a = {} for item in a: count_a[item] = count_a.get(item, 0) + 1 # Check if the elements in list b match the counts in list a for item in b: if count_a.get(item, 0) == 0: return False count_a[item] -= 1 return True a = [3, 1, 2] b = [2, 1, 3] print(check_lists(a, b)) OutputTrue Comment More infoAdvertise with us Next Article Check if Two Lists Have Same Elements regardless of Order - Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python - Check if two lists have at-least one element common Checking if two lists share at least one common element is a frequent task when working with datasets, filtering, or validating data. Python offers multiple efficient ways to solve this depending on the size and structure of the lists.Using set IntersectionConverting both lists into sets and finding 3 min read Python | Check if two lists have any element in common Checking if two lists share any common elements is a frequent requirement in Python. It can be efficiently handled using different methods depending on the use case. In this article, we explore some simple and effective ways to perform this check.Using set IntersectionSet intersection uses Python's 3 min read Python | Check if two list of tuples are identical Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th 4 min read Python - How to Check if two lists are reverse equal Sometimes, while working with Python lists, we can have a problem in which we need to check if two lists are reverse of each other. This kind of problem can have application in many domains such as day-day programming and school programming. Let's discuss certain ways in which this task can be perfo 6 min read Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting 3 min read Like