AlgoMaster Logo

Sets Basics

Last Updated: January 3, 2026

5 min read

Sets are one of those fundamental data structures in Python that often fly under the radar but hold immense power.

They are particularly useful when you need to handle unique elements, like tracking items in an inventory or managing a collection without duplicates.

What is a Set?

At its core, a set is an unordered collection of unique items. Think of it as a bag of distinct items where the order doesn’t matter, and duplicates are not allowed. In Python, sets are defined using curly braces {} or the set() constructor.

Here’s a quick example:

Notice how we only added unique items. If we try to add duplicates, Python will automatically ignore them:

This property of being unique makes sets particularly useful for situations where you want to ensure that all elements are distinct.

Creating Sets

Creating a set can be done in several ways, and understanding these options can help you choose the right one for your needs.

Using Curly Braces

As mentioned earlier, you can create a set using curly braces:

Using the set() Constructor

Alternatively, you can use the set() constructor, which is helpful when converting other iterable types to a set:

Empty Sets

Creating an empty set is slightly different. If you use {}, you'll end up with an empty dictionary instead. Instead, use set():

Sets from Strings

You can also create sets from strings, where each unique character becomes an element:

This can be handy for problems where you want to analyze unique characters in a string.

Accessing Set Elements

While sets are unordered and do not support indexing, you can still access their elements through iteration.

Iterating Over a Set

You can loop through a set using a for loop, which allows you to access each element:

Keep in mind that the order in which elements are returned is not guaranteed. This is an essential aspect of sets that can be a bit confusing initially.

Membership Testing

You can check if an element exists in a set using the in keyword:

This operation is efficient and runs in constant time on average, making it a great choice for lookups.

Practical Use Cases

Sets shine in various real-world applications due to their unique properties. Here are a few scenarios where sets are the go-to solution.

Removing Duplicates

One of the most common uses of sets is to eliminate duplicates from a list:

This is a quick and efficient way to ensure all elements are distinct.

Membership Testing in Large Datasets

When dealing with large datasets, checking for the existence of an item can be a costly operation if done with lists. Using a set simplifies this:

The efficiency of in checks in sets is a key advantage in performance-critical applications.

Set Operations

Even though we will dive deeper into operations in the next chapter, it's worth mentioning that sets support mathematical operations like union, intersection, and differences. These operations can be extremely useful in data analysis tasks.

Common Pitfalls and Edge Cases

While sets are powerful, there are some nuances and potential pitfalls to be aware of.

Immutable Elements Only

Sets can only contain immutable (hashable) items. This means you cannot have lists or other sets as elements:

If you need a collection of sets, consider using frozenset, which is immutable and therefore hashable.

Performance Considerations

While sets are generally efficient, remember that they use more memory than lists due to their underlying implementation. If you're working with a small number of items where duplicates are not a concern, a list might be more appropriate.

Iteration Order

Since sets are unordered, if you need the items in a specific order, you’ll need to sort them after converting to a list:

This ensures you get items in the sequence you want, but remember that it has a performance cost.

Conclusion

Sets are a versatile and powerful tool in Python, perfect for situations where you need to manage collections of unique items efficiently.

You can create them easily, check for membership in constant time, and even remove duplicates from lists with little overhead.

Now that you understand the basics of sets and how to use them effectively, you are ready to explore set methods.

In the next chapter, we will look at the various methods you can call on sets, enhancing your ability to manipulate and utilize them in your projects.