Sets in Python

Sets in Python
by:
blow post content copied from  Real Python
click here to view original post


Python provides a built-in set data type. It differs from other built-in data types in that it’s an unordered collection of unique elements. It also supports operations that differ from those of other data types. You might recall learning about sets and set theory in math class. Maybe you even remember Venn diagrams:

Venn Diagram
Venn Diagram

In mathematics, the definition of a set can be abstract and difficult to grasp. In practice, you can think of a set as a well-defined collection of unique objects, typically called elements or members. Grouping objects in a set can be pretty helpful in programming. That’s why Python has sets built into the language.

By the end of this tutorial, you’ll understand that:

  • A set is an unordered collection of unique, hashable elements.
  • The set() constructor works by converting any iterable into a set, removing duplicate elements in the process.
  • You can initialize a set using literals, the set() constructor, or comprehensions.
  • Sets are unordered because they don’t maintain a specific order of elements.
  • Sets are useful when you need to run set operations, remove duplicates, run efficient membership tests, and more.

In this tutorial, you’ll dive deep into the features of Python sets and explore topics like set creation and initialization, common set operations, set manipulation, and more.

Take the Quiz: Test your knowledge with our interactive “Python Sets” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python Sets

Test your understanding of sets in Python, a commonly used data structure.

Getting Started With Python’s set Data Type

Python’s built-in set data type is a mutable and unordered collection of unique and hashable elements. In this definition, the qualifiers mean the following:

  • Mutable: You can add or remove elements from an existing set.
  • Unordered: A set doesn’t maintain any particular order of its elements.
  • Unique elements: Duplicate elements aren’t allowed.
  • Hashable elements: Each element must have a hash value that stays the same for its entire lifetime.

As with other mutable data types, you can modify sets by increasing or decreasing their size or number of elements. To this end, sets provide a series of handy methods that allow you to add and remove elements to and from an existing set.

The elements of a set must be unique. This feature makes sets especially useful in scenarios where you need to remove duplicate elements from an existing iterable, such as a list or tuple:

Python
>>> numbers = [1, 2, 2, 2, 3, 4, 5, 5]
>>> set(numbers)
{1, 2, 3, 4, 5}

In practice, removing duplicate items from an iterable might be one of the most useful and commonly used features of sets.

Python implements sets as hash tables. A great feature of hash tables is that they make lookup operations almost instantaneous. Because of this, sets are exceptionally efficient in membership operations with the in and not in operators.

Finally, Python sets support common set operations, such as union, intersection, difference, symmetric difference, and others. This feature makes them useful when you need to do some of the following tasks:

  • Find common elements in two or more sets
  • Find differences between two or more sets
  • Combine multiple sets together while avoiding duplicates

As you can see, set is a powerful data type with characteristics that make it useful in many contexts and situations. Throughout the rest of this tutorial, you’ll learn more about the features that make sets a worthwhile addition to your programming toolkit.

Building Sets in Python

To use a set, you first need to create it. You’ll have different ways to build sets in Python. For example, you can create them using one of the following techniques:

In the following sections, you’ll learn how to use the three approaches listed above to create new sets in Python. You’ll start with set literals.

Creating Sets Through Literals

You can define a new set by providing a comma-separated series of hashable objects within curly braces {} as shown below:

Read the full article at https://realpython.com/python-sets/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


May 05, 2025 at 07:30PM
Click here for more details...

=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce