Everything About Python Set Data Structure: Beginner’s Guide

[This article was first published on PyShark, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In this article we will focus on a complete walk through of a Python set data structure.

Table of contents


What is a Python set

A Python set is a data structure for storing an unordered collection of unique elements. A set itself is mutable but consists of elements of immutable data types (such as integer, float, boolean, string, tuple, and others). A set can’t consist of elements of mutable data types (such as list, dictionary, and others).


How to create a Python set

Empty set

To initialize an empty set in Python we can simple run the code below and print its content:

empty_set = set()

print(empty_set)

You should get:

set()

Set with integer elements

When we want to create a set with elements, we can create a comma separated sequence of elements in curly braces:

int_set = {1, 2, 3, 4, 5}

print(int_set)

You should get:

{1, 2, 3, 4, 5}

Set with string elements

Another example would be a set of elements of string data type:

str_set = {"Hello", "Apple", "Car"}

print(str_set)

You should get:

{'Car', 'Apple', 'Hello'}

Set with boolean elements

When working with elements of boolean data type, here is where the property of unique elements in a set is important. Let’s say we have some elements that we want to store, and those are 5 True/False elements. Notice that within the elements, there are only two unique: True and False. If we store these elements in a set, it will be automatically deduplicated:

bool_set = {True, False, True, False, True}

print(bool_set)

You should get:

{False, True}

Notice how repeating elements were dropped from a set.


Set with mixed type elements

Another option is for a set to consist of different elements of immutable data types. For example it can be a string, an integer, a boolean, and a float:

mixed_set = {"Hello", 2, True, 3.01}

print(mixed_set)

You should get:

{True, 2, 3.01, 'Hello'}

Note that the set in unordered based on the element values. What I found interesting however, is that it’s ordered based on the first character of the data type name. Look at this order: {boolean, integer, float, string}, which has an alphabetical pattern.


How to add elements to a Python set

In this section we will discuss ways of adding elements to a Python set.

For this section let’s work with a Python set created below:

my_set = {1, 5, 6}

Add a single element

Let’s say we want to add another integer (8) to the set above. We will need to use the .add() method of a Python set and pass the new element into it:

my_set.add(8)

print(my_set)

And you should get:

{8, 1, 5, 6}

Note: you can only add elements that are not present in the set already. If you try to add an already existing element, like integer 5, you will simply get the same set back, since the element already exists in it.


Add multiple elements

Now let’s consider an example when we want to add multiple new elements to the set above, like integers 3 and 9.

Will the same syntax work? Well, not really. We will need to use the .update() method of a Python set and pass the new elements into it as a set or a list.

Method 1:

my_set.update({3, 9})

print(my_set)

Method 2:

update_elements = [3, 9]
my_set.update(update_elements)

print(my_set)

Both methods will produce identical output:

{1, 3, 5, 6, 8, 9}

How to remove elements from a Python set

In this section we will discuss ways of removing elements from a Python set.

First, let’s create a set that we will be working with:

my_set = {1, 3, 5, 6, 8, 9}

Remove a single element

Let’s say we want to remove one of the elements of this set, for example integer 8. We can easily do it using either .discard() or .remove() methods of the Python set.

Now what is the difference between the two?

The .remove() method removes an element from a set if that element is present in the set, and if it’s not, then it will raise an error (KeyError).

On the other hand, the .discard() method does exactly the same thing, but doesn’t raise the error and leaves the set unchanged.

Method 1:

my_set.remove(8)

print(my_set)

Method 2:

my_set.discard(8)

print(my_set)

Both methods in our case will produce identical output:

{1, 3, 5, 6, 9}

Remove multiple elements

In case we want to remove multiple elements, let’s say integers 3 and 5, we can use similar syntax. The only issue is that .remove() and .discard() methods of the Python set take only one argument per call.

If we create a list of elements to remove, and loop through it, removing them one by one from a set, both of the methods will work.

Method 1:

to_remove = [3, 5]

for element in to_remove:
    my_set.remove(element)
    
print(my_set)

Method 2:

to_remove = [3, 5]

for element in to_remove:
    my_set.discard(element)
    
print(my_set)

Both methods in our case will produce identical output:

{1, 6, 9}

Remove all elements

If we want to remove all elements from any Python set and have an empty set, we can simply call the .clear() method which will remove all elements:

my_set.clear()
    
print(my_set)

And you should get an empty set:

set()

How to iterate over a Python set

In this section we will discuss how to iterate over a Python set.

To get started, let’s create a sample set that we will use:

my_set = {1, 3, 5, 6, 8, 9}

Unlike lists and dictionaries, there aren’t that many options when it comes to iterating over a Python set. And the main one is simply using a for loop:

for element in my_set:
    print(element)

And you should get:

1
3
5
6
8
9

Conclusion

This article is an introductory walkthrough on Python set and its methods which are important to learn as they are used in many areas of programming and in machine learning.

Feel free to leave comments below if you have any questions or have suggestions for some edits.

The post Everything About Python Set Data Structure: Beginner’s Guide appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark.

Want to share your content on python-bloggers? click here.