Exploring Sets in Python

Contents

keyboard_tab
Exploring Sets in Python
Python: Exploring Sets
Overview
Difficulty
Beginner
Duration
7m
Students
6
Description

In this course, we’ll explore set objects a bit closer and cover the ways to create and use sets. This course is part of a series of content designed to help you learn to program with the Python programming language.

Learning Objectives

  • To create an empty set you need to use the built-in set function
  • To create a new set with values you use curly brackets with objects separated with a comma
  • Passing a list to the built-in set function will return a set - thereby removing duplicates from the list
  • Sets are compared using their methods or through the use of operators

Intended Audience

This course was designed for first-time developers wanting to learn Python. 

Prerequisites 

This is an introductory course and doesn’t require any prior programming knowledge.

 

Transcript

Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language. Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: support@cloudacademy.com. And one of our cloud experts will reply.

In this lesson, we’ll explore set objects a bit closer. We’ll be covering the ways to create and use sets. The set object is conceptually similar to the list object. They both contain a collection of objects. However, sets are like lists with superpowers. A key difference is that sets don’t have any sense of object order. Recall that order is an important feature of lists. Lists store objects in the order that we specify. Sets don’t include any concept of order. They’re also incapable of storing duplicate objects. The ability to only store unique objects is a key feature of sets. 

This guarantee of uniqueness gives sets their first superpower. Because they can only store unique objects they can remove duplicates from other collections. Which is something commonly required in many applications. The second superpower of sets is their ability to be compared. We can compare sets in some interesting ways. Here’s a conceptual example of the ways we can compare sets. 

  • We can determine if an object exists in both sets.

    • This is called the intersection. 

  • We can determine if an object exists in only one of the sets.

    • This is called the difference.

  • We can determine if an object exists in one or the other but not both sets.

    • This is called the symmetric difference.

  • We can also merge sets into one set.

    • This is called the union.

We can create set objects using the built-in set function. Calling this function without arguments will create a new empty set. Calling this function and providing a collection is how we can remove duplicates. The function will loop through the collection and return a set containing only unique objects. It’s worth making a mental note here. Because it’s common to need to remove duplicates. To illustrate: we have a list bound to the name letters containing a b c and c. In this example, the letter c is duplicated in the list.

Providing this list as input to the built-in set function will produce a set containing a b c. The shorthand syntax for creating a set looks like a combination of the shorthand used to create lists and dictionaries. Sets are created using curly brackets. Each object is specified inside the brackets and separated using a comma. Recall the dictionaries also use these curly brackets. The difference here is that dictionaries contain key-value pairs. Where sets contain objects. 

There is an issue with reusing these brackets. Specifically when creating an empty set. The interpreter will see an empty pair of curly brackets as a dictionary. Which means, there is no short-hand for creating an empty set. For that, we use the built-in function.

Here we have two sets. One has the letters a b and c. The other has the letters c d and e. So the only letter common to both is c. Let’s see how to compare these two sets. Python provides two options: methods and operators. 

Let’s review some important methods. The add method enables us to add an object to the set. The discard method removes an object from the set. The remove method also removes an object from the set, however, if it doesn’t exist then the interpreter throws an error. The set object also includes methods which are named after each comparison type. There are methods for union, intersection, difference, and symmetric difference. These methods perform the same action as their paired operator. So, I’m going to demonstrate using the operators. 

Let’s start by producing a union of the two sets. This is done using the union operator. Notice it returns a set which includes the values from both sets. And since it returns a set, it automatically removes the duplicated letter c. Notice that the results are unordered because sets don’t have any concept of order. To determine the intersection of set one and set two we can use the intersection operator. This results in a set containing the only letter which exists in both, which is c. We can determine the difference between one and two using the difference operator. Notice this is the subtraction operator. Operators behave differently depending on the object being operated on. 

In previous lessons, we observed this when we used the add operator to combine two strings. Using operators differently depending on the type of object is actually a really fun topic for when you’ve become more comfortable with Python. For now, just make a mental note that operators behave differently for different types of objects. So in the context of sets, this is the difference operator. This will return a set containing the objects in set one that aren’t in set two.

The symmetric difference operator returns a set containing objects in set one and set two, but not both. Since the letter c is the only letter in both, it’s excluded. Sets are common for removing duplicates. However, they’re also quite useful for performing these types of comparisons. 

In these examples, we compared only two sets. However, we can compare multiple sets. Sets do have some limitations regarding the types of objects that can be stored. These restrictions are similar to the restrictions of dictionary’s keys. It’s too much of a rabbit hole to cover now. However, just know there are some limitations. One example being that you can’t store a list inside a set. 

Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:

  • To create an empty set you need to use the built-in set function
  • To create a new set with values you use curly brackets with objects separated with a comma
  • Passing a list to the built-in set function will return a set - thereby removing duplicates from the list
  • Sets are compared using their methods or through the use of operators

That's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!

About the Author
Students
86941
Labs
11
Courses
38
Learning Paths
36

Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.

Covered Topics