How to Use the Python Data Type Set

Athena Ozanich
Athena Ozanich

Published:

Let’s slither into an awesome Python post talking about the data type for collections called sets. There are four types of structures for working with collections of data: list, set, tuple, and dictionary. We won’t go over the other three data types in this post, instead, we are going to set the scene for a better understanding of how Python sets work.

We will also see a few examples of how sets are created, how we can use them, and the benefits they offer.

Young woman studying Python programming and how to use sets.

Without further ado, let's slide right in.

Download Now: An Introduction to Python [Free Guide]

What is a set in Python?

Before we get started, if you haven't checked out our introduction to Python programming language, it's highly recommended. A set is a data collection type used in Python for storing multiple items in a single variable. Sets in Python are unordered and, as such, are not always consistent in the order they get returned. Furthermore, items in the set are immutable — ie. cannot be changed. However, items can be added and removed.

Duplicates are not permitted with Python data sets and will be removed. It is also noteworthy that the Python set supports any data type — including numbers, strings, and booleans — but also has some support for nested data structures.

Set in Python Example

A set in python is identified by the fact that this data type is contained within curly braces, and they are created with them as well. The information stored within the set variable looks like the following example.

 
{"Boa", "Python", "Garter"}

This is what the set data looks like, and it can be stored in a variable for use at other times within your programs.

How to Create a Set in Python

The standard declaration approach for creating a Python set uses curly bracket notation. It is the most commonly used approach and has a very simple syntax. Let's look at that next.

 
snekSet = {"Boa", "Python", "Garter"}

The set constructor is another approach for creating Python sets. This method is more direct and offers other benefits, such as creating more complex set instances.

 
snekSet = set( ("Boa", "Python", "Garter") ) # note the double ()

The next block of code would create three sets, each containing the information added to them on creation.

 
var1 = ("Boa", "Ball", "Garter")
var2 = ("Constrictor", "Snake", "Python")
var3 = ("non-venomous", "venomous", "poisonous")
snekSet= set((var1, var2, var3))

Something interesting to note is that in this case, each set inside of our top-level set will be returned in a randomly generated pattern. However, the sets inside would maintain a preserved order. Let's look at that next in more detail.

If we print the above snekSet to see what it contains, we should see any of the following possible results.

 
{("Boa", "Ball", "Garter"), ("Constrictor", "Snake", "Python"), ("non-venomous", "venomous", "poisonous")}
{("Constrictor", "Snake", "Python"), ("Boa", "Ball", "Garter"), ("non-venomous", "venomous", "poisonous")}
{("non-venomous", "venomous", "poisonous"), ("Constrictor", "Snake", "Python"), ("Boa", "Ball", "Garter")}

The order of each top-level set item is inconsistent, and the larger the set, the more possibilities for combinations. However, if we look at this, we find that it preserves the order of each lower level set, a note-worthy caveat for working with sets in Python.

Methods for Sets in Python

There are several other methods that each function differently and can add to the power that comes with Python sets. Lets' briefly touch on some of these, then step into a recap of this post.

A table of common Python methods and their uses

Manipulating Python Sets

How to Access Sets in Python

Items in a set cannot be targeted by key or index; only by looping through the set can the individual items be accessed. The most common approach is to use the for-in loop, which allows us to iterate over the set once for each item in it.

 
snekSet = {"Boa", "Python", "Garter"}
for x in snekSet:
print(x)

This call to the print function would print each item one at a time for each loop iteration, ending after the last item in the set.

The other method used is the in keyword. If a given item is present within the set, it returns a boolean value. So, for example, the following code block would print true to the console because Python is in the set.

 
snekSet = {"Boa", "Python", "Garter"}
print("Python" in snekSet)

How to Add to Sets in Python

Because sets come with restrictions on manipulating them, such as immutable items and the fact that it does not allow duplicates, adding them is fairly straightforward. Python sets come with a method called add that allows us to directly add an item to the set.

 
snekSet = {"Boa", "Python", "Garter"}
snekSet.add("Viper")

This method adds a single item to the set regardless of if the item is a collection or not, meaning a collection gets added as an item not merged. The next option is to use the update method to update a set with new items. It is important to note that this approach differs from the add method in several ways.

 
snekSet = {"Boa", "Python", "Garter"}
snekColorsSet = {"Red and Black", "multi-tone green"}
snekSet.update(snekColorsSet)
print(snekSet)

The above block of code would update our snekSet with the colors from the snekColorsSet, merging the two sets into a single set. In this case, there are no duplicate items. If there were, update will simply ignore them, and they will not be added.

How to Remove From Existing Sets in Python

Removing items from a set can be accomplished in more ways than adding items can be. The main reason is that sets are unordered, and they return values in random orders. This inconsistency means that removing items can get tricky without targeting the items directly.

Predictable Set Item Removal Methods in Python

To predictably remove items from a Python set, we have the remove and discard methods to specify the item we want to remove. Let's look at the two methods and discuss how they differ.

Python set remove Method

The remove method has a useful feature in that it throws an error if the target item is not present. This error can alert us of unexpected behavior, but we should always prepare to catch that error if it fires.

 
snekSet = {"Boa", "Python", "Garter"}
snekSet.remove("Python")
print(snekSet)

In the case of an error, the following error message can help identify the problem.

 
KeyError: yourTargetKey

Python set discard Method

The discard method differs in that it is less strict, and it will not throw an error if targeted items are not present. The lack of error here has its uses; however, you should still prepare for the possibility of an item not being found as this can cause unexpected behavior in your software.

 
snekSet = {"Boa", "Python", "Garter"}
snekSet.discard("Python")
print(snekSet)

The pop method is different because it simply accesses the set and removes the item designated as last. However, because the order items get returned is unpredictable, there is no accurate way to know what will be removed from the set.

 
snekSet = {"Boa", "Python", "Garter"}
x = snekSet.pop()
print(x)
print(snekSet)

The pop method also returns the value of the item removed, which can be useful for tracking changes made to your sets. In the above code print(x) will print the item being removed, while print(snekSet) will return the remaining items.

Empty or Delete a Set in Python

To remove all items from a set, you can use the clear method, which will clear the set of any items within it but preserve the set itself. The clear method is useful for info dumps and repopulation based on given parameters within your software.

 
snekSet = {"Boa", "Python", "Garter"}
snekSet.clear()

The del keyword serves the same purpose across the board; in any compatible use case, the del keyword deletes the target object. In most other data collection types in Python, you can target an item and delete it. Due to the nature of sets in Python, this is not a feasible action; however, del can still be used to delete a set entirely.

 
snekSet = {"Boa", "Python", "Garter"}
del snekSet

Join Multiple Sets in Python

Joining sets can get tricky. Thankfully, Python has provided us with several functions to facilitate this process in various ways. Let's look at some of these and see how they work with an example of each.

A table of common Python methods and their uses

 

Python Set Union

The union method simply joins two sets into one and returns a new set with all values. If duplicates are present, they will be ignored without an error.

 
snekSet1 = {"Boa", "Python", "Garter"}
snekSet2 = {"Constrictor", "viper", "Boa"}
snekSet3 = set1.union(set2)

Python Set Update

The update method is slightly different because it does not return a new set. Instead, it adds the items from one set to another target set.

 
snekSet1 = {"Boa", "Python", "Garter"}
snekSet2 = {"Constrictor", "viper", "Boa"}
snekSet1.update(snekSet2)

There are other joining methods that behave differently; these are the intersection and symmetric_difference methods.

Python Set Intersection

The intersection method function only looks for duplicate items in multiple sets. It may seem counterintuitive given that sets are not allowed to have duplicates; let's look at how this works.

 
snekSet1 = {"Boa", "Python", "Garter"}
snekSet2 = {"Constrictor", "viper", "Boa"}
snekSet3 = snekSet1.intersection(snekSet2)

In the above example, the only item that would be kept is Boa, as the others are unique among both sets.

 
snekSet1 = {"Boa", "Python", "Garter"
snekSet2 = {"Constrictor", "Viper", "Boa"}
snekSet1.intersection_update(snekSet2)

 And much like the update method, intersection_update changes the original target based on the set provided to the method. So, after running the above code, we would be left with snekSet1 stripped down to only one item.

Python Set Symetric Difference

The symetric_difference method does the opposite; it compares two sets and only keeps the items that are NOT in both.

 
x = {"Boa", "Python", "Garter"}
y = {"Constrictor", "viper", "Boa"}
z = x.symmetric_difference(y)

The symetric_difference_update method is similar to the above however it also shares in the update methods approach of updating an existing set instead of returning a new set altogether.

 
x = {"Boa", "Python", "Garter"}
y = {"Constrictor", "viper", "Boa"}
x.symmetric_difference_update(y)
print(x)

We've covered a lot here, and you should be proud of yourself for making it this far, but there is still more to learn. So next, let's take a look at some of the other methods that could prove useful in your software development needs.

Using Sets in Python

Python sets are unique and have their caveats if you aren't careful, but they come with a lot of power. Some developers would look at this post and immediately identify this as a great data structure for machine learning. Regardless of your purposes, sets are worth understanding, so let's wrap up this post with a quick recap of what we've covered today.

Unordered: Python sets are unordered; therefore, set items cannot be accessed through indexes; they must be targeted directly. Although the items in a set get returned in a random order, Python does offer us the means to target items directly. Python also considers items within a set as keys without paired values.

Immutable: Python sets contain immutable items. While we can still add to and remove from a set, we cannot change items that are already present.

Supports all Data types: Python sets support all data types, including integers, strings, and booleans values, and they can be mixed in any fashion.

Supports nested data: Python sets also support nested data collections. This means we can nest data groups inside a set as individual items.

Many ways to interact: With Python data sets, we have many ways that we can interact with them. However, there are more ways to join sets than adding or removing items.

Caveats to consider: There are a few caveats to consider when working with Python data sets. This biggest caveat is how the unordered nature of sets can affect your software. In addition, with the inability to target items based on their index, you will need to be more careful about how you access data within a set.

With this information at your disposal, you are ready to dive into using Python data sets, try experimenting with them, and learn about how you can manipulate them. Always remember, though, mistakes and errors are just as informative as a lack of them.

 

python

 

Topics: What Is Python?

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

A guide for marketers, developers, and data analysts.

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO