Sets: Software Engineering Full time 13 Phase 3 Hybrid

Close

Learning Goals


Key Vocab


Introduction

Sets are the last built-in data structure in Python. Sets share the following characteristics:

Like lists, tuples, and dictionaries, sets can be defined using their class constructor or with brackets:

my_set = set([1, 2, 3])
my_set = {1, 2, 3}

NOTE: Instantiating an empty set requires the use of the set() class constructor. Closed curly brackets {} will instantiate an empty dictionary.


When Are Sets Used?

Sets are much less commonly used than sequences and maps, but they can be helpful in accomplishing a handful of common tasks:

Instantiating a set using a sequence is a good way to isolate unique members:

my_list = [1, 2, 1, 3, 2]
set(my_list)
# {1, 2, 3}

...though it is important to remember that sets are unordered, so our output might not be pretty:

my_string = "the big red cat ate the fat rat"
set(my_string)
# {'g', 'h', 'b', 'r', 'e', 'd', 'f', 'c', 't', 'a', 'i', ' '}

To determine if two collections have the same members, we can check their sets:

set(range(1, 10)) == set([1, 2, 3, 4, 5, 6, 7, 8, 9])
# True

If the sets are not identical, we can use the intersection & operator to see what they have in common:

set_1 = {1, 2, 3}
set_2 = {3, 4, 5}
set_1 & set_2
# 3

...or we can check for differences using the difference - operator:

set_1 = {1, 2, 3}
set_2 = {3, 4, 5}
set_1 - set_2
# {1, 2}
set_2 - set_1
# {4, 5}

Using these operators also allows us to dynamically modify sets as our program runs:

set_1 = {1, 2, 3}
set_2 = {3, 4, 5}
set_1 &= set_2
# {3}
set_2 -= set_1
# {4, 5}

Sets also support comprehensions with the same syntax as lists:

sentence = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
unique_consonants = {c.lower() for c in sentence if c not in "aeiou ,."}
# {'g', 'p', 'b', 'l', 'r', 'd', 'm', 'q', 'c', 't', 's', 'n'}

For more set features, see the Python set documentation.Links to an external site.


Conclusion

Sets are not the most common data structure in Python, but they can be useful when you need to create and compare unique collections of data. Remember that sets are unique, unordered, and mutable.

Resources