Python Sets — A Powerful Collection Every Developer Should Use

Python Sets — A Powerful Collection Every Developer Should Use

  • Valeria Aynbinder
  • Coding
  • 10 Sep, 2024

Introduction

There are three basic Python collections: list, dictionary, and set. While list and dictionary are widely used in almost every piece of Python code, the set sometimes feels underrated, though it shouldn’t be!

Python set is an incredibly useful collection that allows you to write very efficient and elegant code thanks to its special properties and functionalities.

Mastering sets will significantly improve your code and shorten development time. So, let’s dive deep into Python sets without further ado!

Feel free to watch my video on Python sets with code examples for better understanding.

Let’s start with a motivating task: how do you get unique values from a list that might contain duplicates?

Using a Python set, you can achieve this in a single line of code by creating a set from the given list:

[python set you can do it with a single line of code! Just create a set from the given list]

Note that now our unique_names set does not contain duplicates. This demonstrates one of the two main properties of a set:

Set elements are unique.

Let’s discuss these two main set properties in more detail.

Set Properties

Sets implement two main properties that significantly distinguish them from lists:

  • Set elements are unique
  • Set elements are unordered

At first glance, it might seem odd — why use a collection that doesn’t support ordering and doesn’t allow duplicates when we have lists that don’t have these limitations?

The answer is that these are not limitations, but important properties that can prevent potential bugs and enable unique functionalities. Let’s look at a couple of examples.

Example 1: Cities That Hosted the Olympics

Suppose you want to store all the cities that have ever hosted the Olympic Games. It makes perfect sense to use a set instead of a list:

  • You want each city to appear only once, even if it has hosted multiple times.
  • Ordering isn’t necessary, as there’s no inherent ranking among the cities.

Example 2: Fruits That Grow in Israel

You want to store all the fruits that grow in Israel. The same principles apply:

  • You don’t want duplicates in your collection of fruits.
  • There’s no need for any particular order.

Now let’s see how these properties are implemented in sets.

Uniqueness

In the following code snippet, we have a list of rainy months in Israel from the past three years. Since there are some winter months that often repeat, there are duplicates in the rainy_months_list.

If we want to get a unique set of rainy months in Israel, all we need to do is create a set from rainy_months_list:

Python code to demonstrate uniqueness of elements in set

The set automatically removes all duplicates from the initial collection. Additionally, sets maintain uniqueness by preventing duplicates from being added. In the following code snippet, you can see an attempt to add a duplicate element to the set, but it remains unchanged:

Python code — it is impossible to add duplicates to set

Absence of Order

Since sets don’t support ordering, attempting to access an element at a specific index will raise an exception:

Python code — set does not support indexing

However, we can iterate over set elements like any other collection, though the order of elements during iteration isn’t guaranteed and may change:

Python code — iteration over set

Set Special Functionality

Now that we’ve covered set properties, let’s take a look at some powerful functionalities Python sets implement:

  1. Intersection
  2. Union
  3. Difference

If you’re familiar with Set Theory, these are exactly the operations defined there. If not, this diagram will help you visualize them:

Intersection, Union and Difference

Let’s see these operations in action with some code examples. First, we’ll create three sets to work with:

Create 3 sets. Python.

  • contains all seven weekdays.
  • sport_days represents days when I work out.
  • lecture_days are the days I teach.

We’re going to use set operations to get some insights from these sets.

Intersection

What are the days when I give lectures in the morning and work out in the evening?

To answer this, we need to find elements common to both sport_days and lecture_days, i.e., the intersection of these sets. We can do this in one line using the intersection method:

Intersection example. Python.

Union

On which days do I have something on my schedule (i.e., what are my busy days)?

These are the days present in either sport_days or lecture_days, which matches the definition of a union. Let’s store these as busy_days:

Union example. Python.

Difference

I’m planning a one-day hike with my friends. Which days am I completely free?

The free days are the difference between weekdays and busy_days — days present in weekdays but not in busy_days:

Difference example. Python.

Conclusion

Congrats! You’ve added a powerful tool to your Python toolbox!

Now it’s time to apply what you’ve learned. Consider your recent Python projects. Was there any data that had set properties but was implemented using lists or other collections? If so, try rewriting that code to use sets where appropriate.


You can find all the code presented here on my GitHub.

Thanks for reading!

Tags :
Share :

Related Post

Python Decorators - Deep Dive in 2 Parts (1/2)

Python Decorators - Deep Dive in 2 Parts (1/2)

  • Valeria Aynbinder
  • Coding
  • 12 Jul, 2024

This is a 2-Part Series:Part 1 (current): Intro + Build Your First Decorator Part 2: [Decorators for functions that receive parameters and return values, decorators that receive argu

Read More
Python Decorators - Deep Dive in 2 Parts (2/2)

Python Decorators - Deep Dive in 2 Parts (2/2)

  • Valeria Aynbinder
  • Coding
  • 17 Jul, 2024

This is a 2-Part Series:Part 1: Intro + Build Your First Decorator Part 2 (current): Decorators for functions that receive parameters an

Read More
Django Manage Commands Cheat Sheet

Django Manage Commands Cheat Sheet

  • Valeria Aynbinder
  • Coding
  • 12 Aug, 2024

Installations In order to get started with Django, all you need to do is install Django package using pip: pip install djangoAfter the installation is complete, you can move forward and

Read More
Google Colab - a must-have tool for Developer and Data Scientist

Google Colab - a must-have tool for Developer and Data Scientist

13 amazing features and usage tips.What is Google Colab?Colaboratory, or “Colab” for short, is a product from Google Research. Colab allows an

Read More
Python Regular Expressions - Cheat Sheet

Python Regular Expressions - Cheat Sheet

  • Valeria Aynbinder
  • Coding
  • 16 Oct, 2024

Many code examples + useful tips. Bonus added to the end of the post.Regular expressions is an extremely useful tool, and like any developer, I use it a lot when working with texts. Since I al

Read More