# Sets, FrozenSet, and Multisets

Copied!
Happy Pythoning!

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.

Course Slides (.pdf)

633.9 KB

Sample Code (.zip)

3.1 KB

Alain Rouleau

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!

jackdev

Great course! I’d realised I’d been overcomplicating the data structures in my head and it was making me constantly second-guess myself when designing my hobby projects (not understanding when to move things into classes etc.).

Knowing that applications are “just” data and functions, and data is boiled down into collection-types or record-types made things much simpler. Can just start small with mutable/immutable and scale up from there as needed. Already making a ton more progress on my projects.

Awesome job on this course.

Christopher Trudeau RP Team

I’m glad you enjoyed it. I’ve been coding for a long time, and I started out with fairly structured languages and always thought that was the only way to do things. I was a big fan of setters/getters and private members, protecting the programmer who was using my stuff from the internal workings.

Coming to python was an adjustment. After a while I realized it was a lot of extra effort for very little gain. Who was I protecting? The python idea of using a convention like leading underscores to warn someone but otherwise it was up to them whether to touch it or not was a bit of revelation.

Looking back now, I’m sure a younger me would be baffled by how often I just use dictionaries nowadays. Simplicity if often a lot harder to do that complexity, but frequently worth the extra effort.

Happy coding!

to join the conversation.