A set is a collection of items where each item is unique. This is derived from the mathematical concept of the same name. Python has two built-in types for sets: set
and frozenset
. A set
is a mutable object while frozenset
provides an immutable implementation.
Additionally, the collections
library includes the Counter
object which is an implementation of a multiset, it stores both the unique items and a count as to how many times it has been added to the container.
Here are resources and additonal documentation about sets:
- Built-in Types:Set types – set, frozenset | Python Documentation
- collections – Container Datatypes:Counter objects | Python Documentation
- Sets in Python - Real Python Article
- Sets in Python - Real Python Course
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
Alain Rouleau on March 28, 2021
Really enjoyed the whole concept of a multiset or a so-called bag, thanks. You know, creating a “Counter” object from the “collections” module. Very interesting and something that could actually be useful.
I like not only the idea of a set but how many do I actually have of each? Think of baseball or hockey cards. Not only how many sets do I have but how many cards of each baseball or hockey player do I have in total.
Very powerful!