Last modified: Mar 19, 2026 By Alexander Williams
Python Set Size: Find Length with len()
You need to know how many items are in your Python set. This is called its size or length. It is a common task.
This guide explains how to find the size of a set. We will cover the main method, important details, and practical examples.
What is a Python Set?
A Python set is a built-in data type. It stores a collection of unique items. The items are unordered and unindexed.
Sets are mutable. You can add and remove items. They are perfect for membership tests and removing duplicates. Learn more about their core purpose in our Python Set Function Guide.
How to Find Set Size with len()
Use the built-in len() function. It returns the number of items in any iterable, including sets.
The syntax is simple: len(your_set). It is fast and efficient.
# Example: Find the size of a set
my_set = {10, 20, 30, 40, 50}
set_size = len(my_set)
print(f"The set contains {set_size} items.")
The set contains 5 items.
Checking for an Empty Set
You often need to check if a set is empty. Use len() or evaluate the set directly in a boolean context.
An empty set returns False. A set with items returns True.
# Check if a set is empty
empty_set = set()
non_empty_set = {1, 2, 3}
# Method 1: Using len()
if len(empty_set) == 0:
print("The set is empty (using len).")
# Method 2: Using boolean evaluation (more Pythonic)
if not empty_set:
print("The set is empty (using boolean).")
if non_empty_set:
print(f"The non-empty set has {len(non_empty_set)} items.")
The set is empty (using len).
The set is empty (using boolean).
The non-empty set has 3 items.
Time Complexity of len() on Sets
The len() function on a set has O(1) time complexity. This means it takes constant time.
It does not matter if your set has 10 items or 10 million items. Getting the size is instantly fast.
This is because Python sets store their size in a separate attribute. The len() function just reads this value.
Set Size Changes with Operations
Set size changes when you modify the set. Common operations add or remove items.
For example, the add() method inserts one item. The update() method adds multiple items. You can learn more about adding items in our Python Set Update Method Guide.
To remove items, you can use remove(), discard(), or pop(). The Python Set Pop method removes a random item.
# Demonstrate size change with operations
numbers = {1, 2, 3}
print(f"Initial size: {len(numbers)}")
numbers.add(4) # Add one item
print(f"Size after add(4): {len(numbers)}")
numbers.update([5, 6, 7]) # Add multiple items
print(f"Size after update: {len(numbers)}")
numbers.discard(1) # Remove one item
print(f"Size after discard(1): {len(numbers)}")
# Remove a random item with pop()
removed_item = numbers.pop()
print(f"Removed {removed_item}. New size: {len(numbers)}")
Initial size: 3
Size after add(4): 4
Size after update: 7
Size after discard(1): 6
Removed 2. New size: 5
Common Use Cases for Set Size
Knowing the set size is useful in many situations.
1. Validating Data Uniqueness
You can check if all items in a list are unique. Convert the list to a set. Compare its size to the original list's length.
If the sizes are equal, all items were unique. If the set is smaller, there were duplicates.
# Check for duplicate items in a list
my_list = [5, 3, 5, 8, 3, 1]
my_set = set(my_list)
if len(my_list) == len(my_set):
print("All items in the list are unique.")
else:
print(f"List had duplicates. Unique items: {len(my_set)}")
List had duplicates. Unique items: 4
2. Monitoring Progress in Loops
You can track how many unique items you have processed in a loop. This is helpful for logging or progress bars.
# Track unique processed items
processed_items = set()
data_stream = ["apple", "banana", "apple", "orange", "banana", "grape"]
for item in data_stream:
processed_items.add(item)
current_unique_count = len(processed_items)
print(f"Processed '{item}'. Unique items so far: {current_unique_count}")
Processed 'apple'. Unique items so far: 1
Processed 'banana'. Unique items so far: 2
Processed 'apple'. Unique items so far: 2
Processed 'orange'. Unique items so far: 3
Processed 'banana'. Unique items so far: 3
Processed 'grape'. Unique items so far: 4
3. Comparing Sets
Set size is key when comparing sets. For example, after finding the Python Set Difference, you can check how many items are different.
Similarly, after an intersection, the size tells you how many items two sets share.
# Use size when comparing sets
set_a = {1, 2, 3, 4, 5}
set_b = {4, 5, 6, 7}
common_elements = set_a.intersection(set_b)
different_in_a = set_a.difference(set_b)
print(f"Number of common elements: {len(common_elements)}")
print(f"Number of items only in set_a: {len(different_in_a)}")
Number of common elements: 2
Number of items only in set_a: 3
Important Notes and Best Practices
Remember these points when working with set size.
Sets only store unique items. Adding a duplicate does not change the size. Always use len() to verify.
Use boolean checks for emptiness. Writing if not my_set: is more readable than if len(my_set) == 0:.
len() works on frozen sets too. A frozen set (frozenset) is immutable, but you can still get its size with len().
For a complete overview of how to modify sets, see our Python Set Methods Guide.
Conclusion
Finding the size of a Python set is simple. Use the len() function. It is fast and reliable.
You now know how to check for empty sets. You understand that size changes with operations like add() or pop().
Use set size to validate data, track progress, and compare collections. It is a fundamental tool for effective Python programming.
Keep your code clean. Use boolean checks for emptiness. Leverage the O(1) speed of len() for all your set size needs.