PYTHON
Using Sets for Fast Membership Testing and Deduplication
Master Python sets for highly efficient membership testing (checking if an item exists) and deduplicating lists, essential for optimizing data processing and validation tasks.
# Deduplication
data_with_duplicates = [1, 2, 2, 3, 4, 4, 5, 1]
unique_data = list(set(data_with_duplicates))
print(f"Deduplicated list: {unique_data}")
# Expected output: Deduplicated list: [1, 2, 3, 4, 5] (order not guaranteed)
# Fast Membership Testing
allowed_roles = {"admin", "editor", "viewer"}
user_role = "editor"
if user_role in allowed_roles:
print(f"User with role '{user_role}' is allowed access.")
else:
print(f"User with role '{user_role}' is NOT allowed access.")
# Another example: checking for common elements
list_a = [1, 2, 3, 4]
list_b = [3, 4, 5, 6]
common_elements = set(list_a) & set(list_b) # Intersection
print(f"Common elements: {list(common_elements)}")
# Expected output: Common elements: [3, 4]
How it works: Python sets are unordered collections of unique elements. They are optimized for fast membership testing (`item in my_set`) with an average time complexity of O(1), making them ideal for checking if an element exists. Sets are also perfect for deduplicating lists by converting the list to a set and back to a list, as sets inherently store only unique items. Set operations like union (`|`), intersection (`&`), and difference (`-`) are also highly efficient.