Sets

Sets are the last data structure used to store data that we will see. It is similar to lists, but it won’t allow duplicate items.

Sets are, basically, non ordered lists or collections of unique items.

Creating and accessing data in sets

To create a set, we can use the set() constructor, which will accept sequences (lists, tuples, etc), into sets. Another possibility is to define the values inside of curly brackets:

list1 = ["Luiz", "Alfredo", "Felipe", "Alfredo", "Joana", "Carolina", "Carolina"]
set1 = set(list1)
print(set1)
set2 = {"dog", "cat", "parrot", "dog", "parrot", "monkey", "chicken"}           
print(set2)

> {'Joana', 'Alfredo', 'Felipe', 'Luiz', 'Carolina'}
> {'monkey', 'cat', 'dog', 'chicken', 'parrot'}

As we previously saw, strings in Python are also treated as lists, and as such, can be used on the set() constructor to create a set:

set3 = set("parrot")
print(set3)

> {'o', 'r', 'p', 'a', 't'}

One of the most common use cases for sets are to remove duplicate items from lists:

list2 = ["Luiz", "Alfredo", "Felipe", "Alfredo", "Joana", "Carolina", "Carolina"]
list_without_duplicates = list(set(list2))
print(list_without_duplicates)

> ['Joana', 'Alfredo', 'Felipe', 'Luiz', 'Carolina']

To add an item to a set, we have the add() function:

set1.add("Joca")
print(set1)

> {'Joana', 'Alfredo', 'Felipe', 'Luiz', 'Carolina', 'Joca'}

The update() function adds all items from an iterable into the set:

set1.update(["Rafael", "Pedro", "Alice"])
print(set1)

> {'Rafael', 'Alice', 'Joana', 'Alfredo', 'Felipe', 'Luiz', 'Pedro', 'Carolina', 'Joca'}

And finally, it’s possible to concatenate two sests using the | operator:

print(set1 | set2)

> {'Rafael', 'macaco', 'Alice', 'cachorro', 'papagaio', 'Joana', 'Alfredo', 'gato', 'Felipe', 'galinha', 'Luiz', 'Pedro', 'Carolina', 'Joca'}

To remove values from a sert, we use remove(). This function receives the value to be removed as an argument, and then removes it from the set:

print(set2)
set2.remove("dog")
print(set2)

> {'monkey', 'cat', 'dog', 'chicken', 'parrot'}
> {'monkey', 'cat', 'chicken', 'parrot'}

Some functions to work with sets

As with other data structures, len() works with sets, returning the number of elements in it. We can also use in to check if a certain item belongs to a set:

print(len(set2))
print("cat" in set2)
print("elephant" in set2)

> 4
> True
> False

Besides that, we also have other more specific features to help us work with sets.

difference() e intersection()

The difference() and intersection() return, the difference and the intersection between two sets:

set4 = {"Luiz", "Alfredo", "Joana", "Felipe", "Mauro"}
set5 = {"Joana", "Carolina", "Afonso", "Carlos", "Mauro"}
print(set4.difference(set5))
print(set4.intersection(set5))

> {'Luiz', 'Felipe', 'Alfredo'}
> {'Joana', 'Mauro'}

copy()

As with lists, to copy a set we use the same copy() function. Creating a set through assigning it to a new variable will only create a new reference to the data structure. Let’s see how assignment won’t work:

set_backup = set4
print(set_backup)
set4.clear()
print(set_backup)

> {'Mauro', 'Alfredo', 'Felipe', 'Joana', 'Luiz'}
> set()

And now, let’s see how copy() makes the right thing and works as we expect:

set4 = {"Luiz", "Alfredo", "Joana", "Felipe", "Mauro"}
set_backup = set4.copy()                               
print(set_backup)
set4.clear()
print(set_backup)

> {'Alfredo', 'Mauro', 'Luiz', 'Joana', 'Felipe'}
> {'Alfredo', 'Mauro', 'Luiz', 'Joana', 'Felipe'}

Conclusion

And, with this, we finish the sets chapter, and we also saw every iterable data structure, with lists, tuples and sets. We saw how to create sets, saw that we can’t access items through an index but we can iterate over the items in a set. Finally, we saw how to manipulate sets, how to add and remove items from it and more. In the next chapter, we will talk about control structures.