← Back to all snippets
PYTHON

Efficiently Remove Duplicates and Find Intersections with Python Sets

Utilize Python sets to quickly remove duplicate items from lists and find common or unique elements between collections, enhancing data cleaning and validation tasks.

# Removing duplicates from a list
data_with_duplicates = [1, 2, 2, 3, 4, 4, 5, 1, 6]
unique_data = list(set(data_with_duplicates))
print(f"Original list: {data_with_duplicates}")
print(f"Unique data: {unique_data}")

# Finding common elements (intersection)
tags_user1 = {'python', 'webdev', 'flask', 'api'}
tags_user2 = {'javascript', 'react', 'webdev', 'api'}
common_tags = tags_user1.intersection(tags_user2)
# Alternatively: common_tags = tags_user1 & tags_user2
print(f"Tags for User 1: {tags_user1}")
print(f"Tags for User 2: {tags_user2}")
print(f"Common tags: {common_tags}")

# Finding unique elements (union)
all_tags = tags_user1.union(tags_user2)
# Alternatively: all_tags = tags_user1 | tags_user2
print(f"All unique tags: {all_tags}")

# Finding elements in one set but not the other (difference)
user1_exclusive_tags = tags_user1.difference(tags_user2)
# Alternatively: user1_exclusive_tags = tags_user1 - tags_user2
print(f"Tags exclusive to User 1: {user1_exclusive_tags}")

# Checking for subsets and supersets
set_a = {1, 2, 3}
set_b = {1, 2, 3, 4, 5}
print(f"Is set_a a subset of set_b? {set_a.issubset(set_b)}")
print(f"Is set_b a superset of set_a? {set_b.issuperset(set_a)}")
How it works: This snippet demonstrates the power of Python's `set` data structure for efficient data manipulation. Sets automatically store only unique elements, making them ideal for removing duplicates from lists. It also shows how to perform common set operations like `intersection()` (finding common elements), `union()` (combining unique elements), and `difference()` (finding elements exclusive to one set), which are invaluable for filtering and comparing data in web applications.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs