Python Progamming/Python Data Structures

Python Sets

Updated 3/15/2026
1 min read

In the previous article, you learned about tuples, which store ordered collections of values that cannot be modified after creation. Tuples are useful when a program needs to represent fixed groups of related data, such as coordinates or structured records. However, not every collection of data needs to preserve order. In some situations, the most important property of a collection is uniqueness.

For example, a program might need to store a list of unique user IDs, track which pages a visitor has viewed, or remove duplicate values from a dataset.

Python provides a data structure designed specifically for these situations: the set.

What Is a Set?

A set is an unordered collection of unique elements. Unlike lists or tuples, sets automatically eliminate duplicate values.

For example:

numbers = {1, 2, 3, 3, 4}
print(numbers)

Even though the value 3 appears twice in the definition, the resulting set contains only one instance of that value.

Sets are useful when the program must ensure that each element appears only once.

Creating Sets

Sets are typically created using curly braces {} with values separated by commas.

fruits = {"apple", "banana", "orange"}

Another way to create a set is by using the set() function. This is especially useful when converting other collections into sets.

For example:

numbers = [1, 2, 2, 3, 4]
unique_numbers = set(numbers)

print(unique_numbers)

Here the list contains duplicate values, but converting it into a set automatically removes them.

Unordered Nature of Sets

One important characteristic of sets is that they do not maintain order. Unlike lists or tuples, the position of elements in a set is not guaranteed.

For example:

items = {"apple", "banana", "orange"}
print(items)

The output order may differ each time the program runs.

Because sets are unordered, elements cannot be accessed using indexes. Instead, sets are typically used for membership checks or mathematical set operations.

Modifying Sets

Although sets are unordered, they are mutable, which means elements can be added or removed.

For example:

python
1fruits = {"apple", "banana"}
2
3fruits.add("orange")
4print(fruits)

The add() method inserts a new element into the set.

Elements can also be removed using methods such as remove() or discard().

fruits.remove("banana")
print(fruits)

These operations allow programs to maintain dynamic collections of unique values.

Set Operations

Sets support several operations inspired by mathematical set theory. These operations allow programs to compare and combine collections efficiently.

For example:

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)

This operation performs a union, producing a set that contains all elements from both sets.

Python supports several common set operations:

  • Union (|) combines elements from both sets
  • Intersection (&) returns elements common to both sets
  • Difference (-) returns elements present in one set but not the other
  • Symmetric difference (^) returns elements that appear in one set but not both

These operations are powerful tools for comparing and manipulating collections of data.

Membership Testing With Sets

Sets are particularly efficient for checking whether an element exists within a collection.

For example:

users = {"alice", "bob", "charlie"}

print("alice" in users)

Membership checks in sets are typically faster than similar checks in lists, especially when the collection becomes large.

For this reason, sets are often used when programs need to track unique items and test membership frequently.

Sets in Real Programs

Sets are widely used in applications that require unique values.

  • A web application might use sets to track unique visitors to a page.
  • A data-processing script might use sets to remove duplicate entries from a dataset.
  • Security systems may store sets of authorized users or permissions.

In each of these cases, the guarantee of uniqueness simplifies the logic and prevents errors caused by duplicate data.

Why Sets Matter

Sets provide an efficient way to manage collections where uniqueness is important. Their built-in operations make it easy to compare groups of data, remove duplicates, and perform membership checks quickly. By using sets appropriately, programs can simplify data-processing tasks and avoid unnecessary complexity.

Understanding sets therefore expands the range of problems that a program can solve effectively.

What Comes Next

Now that you understand how sets manage collections of unique elements, the next step is to explore a data structure designed for mapping relationships between values.

In the next article, you will learn about Dictionaries in Python, which store data as key–value pairs and allow programs to associate one piece of information with another. Because many real-world problems involve not just collections of values, but relationships between them.

Python Sets | Learn Syntax | Learn Syntax