Python - Check if list contains all unique elements Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report To check if a list contains all unique elements in Python, we can compare the length of the list with the length of a set created from the list. A set automatically removes duplicates, so if the lengths match, the list contains all unique elements. Python provides several ways to check if all elements in a list are unique, Let’s explore the best methods to achieve this.Using set()Sets in Python inherently store unique values. By converting the list to a set, we can compare its length with the original list. Python # Check if a list contains all unique elements a = [1, 2, 3, 4, 5] # Compare lengths of the list and the set b = len(a) == len(set(a)) print(b) OutputTrue Explanation:The set(a) removes duplicates automatically.If the lengths of the set and list are equal, all elements in the list are unique.Let's explore some more methods to check if list contains all unique elements.Table of ContentUsing a Loop with a Set Using the collections.CounterUsing Nested LoopsUsing a Loop with a SetWe can iterate through the list and check for duplicates using a set to keep track of seen elements. Python a = [1, 2, 3] seen = set() # Check for duplicates unique = True for x in a: if x in seen: unique = False break seen.add(x) print(unique) OutputTrue Explanation:A set keeps track of elements we’ve already seen.If an element is found in the set during iteration, the list contains duplicates. Using the collections.CounterThe Counter class from collections can count the occurrences of each element in the list. Python from collections import Counter a = [1, 2, 3, 4, 5] counts = Counter(a) # Check if all elements appear only once unique = all(value == 1 for value in counts.values()) print(unique) OutputTrue Explanation:Counter(a) creates a dictionary where keys are elements and values are their counts.If all counts are 1, the list has all unique elements.Using Nested LoopsThis is the brute-force approach which compares every element with every other element. Python a = [1, 2, 3, 4, 5] x = all(a[i] != a[j] for i in range(len(a)) for j in range(i + 1, len(a))) print(x) OutputTrue Explanation:The nested loop checks every pair of elements for equality. Comment More infoAdvertise with us Next Article Python - Find all elements count in list M manjeet_04 Follow Improve Article Tags : Python python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python - Find all elements count in list In Python, counting the occurrences of all elements in a list is to determine how many times each unique element appears in the list. In this article, we will explore different methods to achieve this. The collections.Counter class is specifically designed for counting hashable objects. It provides 3 min read Python - Find all elements count in list In Python, counting the occurrences of all elements in a list is to determine how many times each unique element appears in the list. In this article, we will explore different methods to achieve this. The collections.Counter class is specifically designed for counting hashable objects. It provides 3 min read Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the 3 min read Check if element exists in list in Python In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example:Pythona = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Element exists in the 3 min read Python program to check if a string contains all unique characters To implement an algorithm to determine if a string contains all unique characters. Examples: Input : s = "abcd" Output: True "abcd" doesn't contain any duplicates. Hence the output is True. Input : s = "abbd" Output: False "abbd" contains duplicates. Hence the output is False. One solution is to cre 3 min read Python program to check if a string contains all unique characters To implement an algorithm to determine if a string contains all unique characters. Examples: Input : s = "abcd" Output: True "abcd" doesn't contain any duplicates. Hence the output is True. Input : s = "abbd" Output: False "abbd" contains duplicates. Hence the output is False. One solution is to cre 3 min read Like