Last modified: Mar 19, 2026 By Alexander Williams

Python Set Function Guide: Unique Data

Python's set() function is a powerful tool. It creates an unordered collection of unique items. This is essential for many programming tasks.

Sets help you manage data without duplicates. They are fast for membership tests. This guide will show you how to use them effectively.

What is a Python Set?

A set is a built-in data type. It represents an unordered collection. The key feature is that all elements must be unique.

You can create a set using curly braces {} or the set() function. The set() function is more versatile. It can convert other iterables.

Sets are mutable. You can add and remove items. But the items themselves must be immutable, like numbers, strings, or tuples.


# Creating sets in Python
my_set = {1, 2, 3, 4, 5}  # Using curly braces
another_set = set([1, 2, 2, 3, 3, 4])  # Using set() function on a list
empty_set = set()  # Creates an empty set. {} creates an empty dictionary.

print("my_set:", my_set)
print("another_set:", another_set)  # Duplicates are removed
print("empty_set:", empty_set)
    

my_set: {1, 2, 3, 4, 5}
another_set: {1, 2, 3, 4}
empty_set: set()
    

Why Use the Set Function?

The primary use is removing duplicates. It's a one-line solution for cleaning data. This is more efficient than manual loops.

Sets provide fast membership testing. Checking if an item is in a set is very quick. It's much faster than checking a list.

They are also perfect for mathematical operations. Think of union, intersection, and difference. These are common in data analysis.


# Practical example: Removing duplicates from a list
customer_ids = [101, 102, 101, 103, 102, 104, 101]
unique_ids = set(customer_ids)  # Convert list to set
clean_list = list(unique_ids)   # Convert set back to list (optional)

print("Original list:", customer_ids)
print("Unique IDs (set):", unique_ids)
print("Clean list:", clean_list)
    

Original list: [101, 102, 101, 103, 102, 104, 101]
Unique IDs (set): {101, 102, 103, 104}
Clean list: [101, 102, 103, 104]
    

Essential Set Methods and Operations

Once you have a set, you need to work with it. Python provides many methods. Let's explore the most important ones.

Adding and Removing Items

Use add() to insert a single item. Use update() to add multiple items from another iterable. Our guide on the Python Set Insert: How to Add Items covers this in detail.

To remove items, you have options. remove() raises an error if the item is missing. discard() does not. The pop() method removes a random item. Learn more about safe deletion in our article on Python Set Remove: Delete Items Safely.


# Adding and removing items
fruits = {"apple", "banana"}
fruits.add("orange")        # Add a single item
print("After add:", fruits)

fruits.update(["grape", "kiwi", "banana"])  # Add multiple items, 'banana' is ignored
print("After update:", fruits)

fruits.remove("apple")      # Removes 'apple', error if not found
print("After remove:", fruits)

fruits.discard("mango")     # Tries to discard 'mango', no error
print("After discard:", fruits)

removed_item = fruits.pop() # Removes and returns a random item
print(f"Popped: {removed_item}, Remaining: {fruits}")
    

After add: {'apple', 'banana', 'orange'}
After update: {'apple', 'banana', 'orange', 'grape', 'kiwi'}
After remove: {'banana', 'orange', 'grape', 'kiwi'}
After discard: {'banana', 'orange', 'grape', 'kiwi'}
Popped: banana, Remaining: {'orange', 'grape', 'kiwi'}
    

Set Theory Operations

Sets shine in mathematical operations. You can find common elements, differences, and combine sets. These operations are very efficient.

Use union() (or |) to combine sets. Use intersection() (or &) to find common items. Use difference() (or -) to find items in one set but not another. For a complete overview, see our Python Set Operations Guide.


# Set operations example
set_a = {1, 2, 3, 4, 5}
set_b = {4, 5, 6, 7, 8}

union_set = set_a.union(set_b)          # Or: set_a | set_b
intersection_set = set_a.intersection(set_b) # Or: set_a & set_b
difference_set = set_a.difference(set_b)    # Or: set_a - set_b

print("Set A:", set_a)
print("Set B:", set_b)
print("Union (A | B):", union_set)
print("Intersection (A & B):", intersection_set)
print("Difference (A - B):", difference_set)
    

Set A: {1, 2, 3, 4, 5}
Set B: {4, 5, 6, 7, 8}
Union (A | B): {1, 2, 3, 4, 5, 6, 7, 8}
Intersection (A & B): {4, 5}
Difference (A - B): {1, 2, 3}
    

Frozenset: The Immutable Set

Python also offers frozenset(). It creates an immutable set. You cannot add or remove items after creation.

This is useful when you need a hashable set. Regular sets are not hashable. They cannot be used as dictionary keys or elements in another set. A frozenset can.


# Working with frozenset
immutable_set = frozenset([10, 20, 30, 20])
print("Frozenset:", immutable_set)
print("Type:", type(immutable_set))

# This will cause an error if uncommented:
# immutable_set.add(40)  # AttributeError: 'frozenset' object has no attribute 'add'

# Using frozenset as a dictionary key
valid_dict = {immutable_set: "This is a valid key"}
print(valid_dict)
    

Frozenset: frozenset({10, 20, 30})
Type: 
{frozenset({10, 20, 30}): 'This is a valid key'}
    

Common Use Cases and Best Practices

Sets are perfect for finding unique items in data. Use them when order does not matter. They are ideal for membership tests.

Remember, sets are unordered. Do not rely on the order of items. The order may change between Python runs or versions.

Use sets for large collections where you need to check "is this item present?" often. It's their strongest advantage over lists.

Conclusion

The Python set() function is a fundamental tool. It creates efficient, unique collections. Mastering sets will improve your code's performance and clarity.

Use sets for deduplication, fast lookups, and mathematical operations. Remember the key methods like add, remove, union, and intersection.

Start using sets in your projects today. They solve common problems with simple, elegant solutions. Your data processing tasks will become much easier.