Python Sets are fundamental data structures designed specifically to store unique, unordered elements. Because sets automatically discard duplicates upon creation, they are the most efficient way to check for uniqueness or to remove redundant data from other collections (like lists).
This guide explains the inherent uniqueness property of sets, how to verify uniqueness using length comparison, and how to perform membership checks efficiently.
Understanding Set Uniqueness
A set is defined using curly braces {} or the set() constructor. Its defining characteristic is that it cannot contain duplicate elements.
numbers = {1, 2, 2, 3, 4, 4, 5}
print(numbers)Output:
{1, 2, 3, 4, 5}Method 1: Checking if a List has Unique Elements
A common interview question or data validation task is: "Does this list contain only unique elements?"
You can solve this by comparing the length of the original list with the length of the set created from that list. Since sets remove duplicates, if the lengths are equal, the list was already unique.
def has_unique_elements(data):
return len(data) == len(set(data))
list_unique = [1, 2, 3, 4, 5]
list_duplicate = [1, 2, 2, 3, 4]
print(f"List 1 unique? {has_unique_elements(list_unique)}")
print(f"List 2 unique? {has_unique_elements(list_duplicate)}")Output:
List 1 unique? True
List 2 unique? FalseMethod 2: Membership Testing with in
Sets are optimized for checking if an element exists. While searching a list takes linear time O(N), checking a set takes constant time O(1) on average.
my_set = {10, 20, 30}
if 20 in my_set:
print("20 is present.")
if 99 not in my_set:
print("99 is NOT present.")Output:
20 is present.
99 is NOT present.Conclusion
To handle uniqueness in Python:
- Use
set(data)to automatically remove duplicates. - Compare
len(list)vslen(set(list))to verify if a collection contains only unique elements. - Use sets for high-performance membership testing (
if x in my_set).