Unlocking Python Sets: The Secret to fast Membership Tests and Unique Data Handling

Manish KumarManish Kumar
5 min read

Introduction

So, let’s start by asking ourselves a question: What comes to mind when you hear the word “sets”?

For many of us, it brings back memories of our school days, when we were first introduced to the concept of sets. In mathematics, a set is defined as an organized collection of distinct objects, often represented using set-builder notation.
For example: S = {1, 2, 3, 4}

In Python, sets are also available as a built-in data type, though with some unique characteristics and functionality.

Let’s dive deeper into the concept of sets in Python, explore the various operations you can perform on them.

What are Sets in Python ?

Sets are unordered collections of items that can be of any data type. They are mutable, meaning you can add or remove elements from a set.

However, there's a catch—you'll discover some interesting behaviors and limitations when we dive into how sets are created and how their elements are accessed.

How to create and access Sets in Python ?

1. Initialization of Sets

Sets are unordered collection of Unique elements.

2. Creating using Set() function

Sets can also be created using the built-in Set() function, which takes an iterable (a collection of items) as an argument and returns a set containing only the distinct elements from that iterable.

3. Creating an Empty Python Set

Built-In Methods in Python Sets

1. Length of set

2. Accessing Elements of set

Since sets cannot be accessed using index values, you can use a for-loop to iterate over the elements and access them one by one.

3. Adding Elements in an existing set

  • add() Method: Adding single element in the set.

  • update() Method: Adding multiple elements in the set.

4. Removing Element from the set

  • set.remove() Method: It takes one parameter the element to be removed—and then removes that element from the given set.

    However, if the specified element is not present in the set, it raises a keyError.

  • set.discard() Method: It takes one parameter the element to be removed—and then removes that element from the given set.

    However, if the specified element is not present in the set, it doesn’t raises a KeyError. Therefore set.discard() better than set.remove() method if you doesn’t sure about the element inside set.

  • set.clear() Method: Clear all the elements from the set.

  • set.del() Method: When we want to completely delete the set.

Important Note : In sets, you cannot access elements using an index because the elements are stored in an unordered manner. Overall, sets are mutable, meaning you can add or remove elements from them. However, here's an interesting fact, the elements inside a set must be immutable. This means you cannot store mutable data types like lists or dictionaries inside a set. Attempting to do so will result in an error.

Mathematical Operations on Python Sets

1. Union of Sets

The union of two sets A and B is defined as the set containing the elements that are present in A, B, or both and is denoted by A B.

  • Using the "|" operator

  • Using the set.union() Method

2. Intersection of Sets

The intersection of sets A and B is defined as the set containing elements that are common in both A and B and is denoted by A ∩ B.

  • Using "&" operator

  • Using the set.intersection() Method

NOTE: Distinction between intersection and intersection_update() that is intersection method returns the new set that exist in both sets i.e set 1 and set 2. Whereas intersection_update() removes the element that are not exists in both the sets.

3. Difference of Sets

Difference between set A and B is defined as elements of set A that is not present in set B. It is denoted by A-B.

  • Using the '-' operator

  • Using set.difference() Method

NOTE: Distinction between difference and difference_update() that is difference method returns the new set that not exist in both sets but present in set 1. Whereas difference_update() removes the element that are exists in both the sets i.e set 1 and set 2 .

4. Symmetric difference of Sets

Symmetric difference of two sets A and B is defined as the set of elements that are in either set A and set B but not present in both and it is denoted by A △ B.

  • Using the ' ^ ' operator

  • Using set.symmetric_difference() Method

    NOTE: Distinction between symmetric_difference and symmetric_difference_update() that is symmetric_difference method returns the new set with all the elements of the set, but are not common in both the sets i.e. set 1 and set 2. Whereas symmetric_difference_update() removes the element that are present in both the sets i.e. set 1 and set 2 and inserting remaining elements.

Conclusion

This article introduced the fundamentals of Python sets—what they are, how to create them, and some of their key attributes. We also explored various operations such as adding and removing elements, along with performing mathematical set operations. When comparing Python sets to lists, sets are best suited for storing unique, unordered elements.

#ChaiCode

0
Subscribe to my newsletter

Read articles from Manish Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manish Kumar
Manish Kumar