Sets In Python - Real Python

Table of Contents

  • Getting Started With Python’s set Data Type
  • Building Sets in Python
    • Creating Sets Through Literals
    • Using the set() Constructor
    • Using Set Comprehensions
  • Performing Common Set Operations
    • Union
    • Intersection
    • Difference
    • Symmetric Difference
  • Using Augmented Set Operations
    • Augmented Union
    • Augmented Intersection
    • Augmented Difference
    • Augmented Symmetric Difference
  • Comparing Sets
    • Subsets
    • Proper Subsets
    • Supersets
    • Proper Supersets
    • Disjoint Sets
  • Using Other Set Methods
    • Adding an Element With .add()
    • Removing an Existing Element With .remove()
    • Deleting an Existing or Missing Element With .discard()
    • Removing and Returning an Element With .pop()
    • Removing All Elements With .clear()
    • Creating Shallow Copies of Sets With .copy()
  • Traversing Sets
    • Accessing and Modifying Elements in a Loop
    • Processing and Removing Elements in a Loop
    • Iterating Through a Sorted Set
  • Exploring Other Set Capabilities
    • Finding the Number of Elements With len()
    • Running Membership Tests on Sets
  • Conclusion
  • Frequently Asked Questions
Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using Sets in Python

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.

Get Your Code: Click here to download the free sample code that shows you how to work with sets in Python.

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:

Sets in Python

Interactive Quiz

Python Sets

In this quiz, you'll assess your understanding of Python's built-in set data type. You'll revisit the definition of unordered, unique, hashable collections, how to create and initialize sets, and key set operations.

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}

Tag » How To Add To A Set Python