Basic Python Data Structures: Sets

Rafay Syed
5 min readSep 15, 2019

We have looked at lists and tuples, but there is another data structure called Sets. A set is also mutable, meaning that its contents can change just as a list. It can add and remove elements. You must be asking, “why don’t we just use a list if sets can also be mutable?” There are some differences that I will go over and explain why sets are used in certain scenarios. An example of a set is shown below.

Let’s start off with the syntax. A set will contain values in curly braces ({}) and each value is separated by a comma, same as lists and tuples. Now here is one big discrepancy. When you print out a set, the order of the values in the set will not be the same. If you expect that 1, 6, 17, 9, 3 will be printed out, you are mistaken. This is how the numbers were printed out in the IDE that I was running it on:

Instead, the order was printed out as 1, 3, 9, 6, 17. A set will output a random order of the values that are in it.

Another key difference is that sets DO NOT CONTAIN duplicate values. For example, let’s say this was my set:

In that set, “Scott” is repeated twice. But when names gets printed out, this is the output:

Even though I had 5 values in the original “names” set, only four values were in the output set. This is because “Scott” was mentioned twice in the names set. You can also see that the order of the values are random.

Advanced Set Operations

With sets, we can use them to print out all sorts of data. If we want to find the difference between two sets, we can use an operation called “difference”. If we want to find members in both sets, we use “intersection”. If we want to find all members from both sets, we use “union”. If we want to find members that are not in both sets, we can use something called a “symmetric difference”. I will demonstrate each of these operations. The two sets below are the sets that we will be using for each of these operations.

Difference

The variable math_but_not_history will compare each sets to see which friends are ONLY in the math class and not in the history class as well. Hence the operation difference. The second variable history_but_not_math will only look for friends that are in the history class but not the math class as well. Here are the outputs for both print statements.

math_but_not_history result
history_but_not_math result

Symmetric Difference

In the not_in_both_classes variable, the output will result in a set that does not contain students in both classes. This is what the symmetric difference operation does. The result is shown below.

not_in_both_classes result

Brad, Jack and Charles are the only members not in both sets, but Fred is. Therefore, Fred is not in the output.

Intersection

In art_and_science, the intersection operation is being used, which allows us to find members from both sets. Since Fred is the only student in both sets, Fred will be printed out.

art_and_science result

Union

In a union, all the values from both sets are printed out. The result is shown below.

all_students

If you remember earlier, a set will only contain unique values, so even though Fred is in both sets, he is only mentioned once.

Take Aways

  1. Sets will only contain unique values. Therefore, duplicates will be taken out in the resulting set.
  2. Sets are mutable, meaning they can change. The method you can use for adding a value to a set is add and you can remove a value by using the remove method, similar to a list.
  3. Sets will print out a random order of the values that are in them. If you have a set that contains values {14, 51, 21, 32, 9, 0}, it can be printed as {51, 21, 0, 9, 14, 32} or {21, 0, 51, 32, 0, 9} or in many other ways.

The next and final data structure in this series will be the dictionary, which will be exciting and fun!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Rafay Syed
Rafay Syed

Written by Rafay Syed

Software Engineer at Salesforce and Lifelong Student

No responses yet

Write a response