Specialized Collections
Use specialized Python collections to create more efficient programs!
StartSets
Lesson 1 of 2
- 1In Python, a set is a group of elements that are unordered and do not contain duplicates. Although it may seem that the usefulness of this data structure is limited, it can actually be very helpful…
- 2In Python, there are multiple ways to create a set. A set object can be created by passing an iterable object into its constructor, using curly braces, or using a set comprehension. Let’s examine …
- 3Unlike a normal set, you can only create a frozenset using its constructor. Remember that using a frozenset means that you cannot modify the elements inside of it. Creating a frozenset using its c…
- 4There are two different ways to add elements to a set: 1. The .add() method can add a single element to the set. # Create a set to hold the song tags song_tags = {‘country’, ‘folk’, ‘acoust…
- 5There are two methods for removing specific elements from a set: 1. The .remove() method searches for an element within the set and removes it if it exists, otherwise, a KeyError is thrown. …
- 6In Python, set and frozenset items cannot be accessed by a specific index. This is due to the fact that both containers are unordered and have no indices. However, like most other Python containers…
- 7A lot of the usefulness of a set container comes from the set operations. These allow you to combine sets, find the difference and intersections of sets, and more! You can combine these operations …
- 9Let’s say that we have two or more sets, and we want to find which items both sets have in common. The set container has a method called .intersection() which returns a new set or frozenset consist…
- 10Similar to how we can find elements in common between sets, we can also find unique elements in one set. To do so, the set or frozenset use the .difference() method or the - operator. This returns…
- 11The last operation we will be looking at is the symmetric difference. We can think of this operation as the opposite of the intersection operation. A resulting set will include all elements from th…
- 12Great Job! You have learned about many different ways to work with set and frozenset containers! We looked at: Creating a set or frozenset: * For set containers, we can use curly braces {}, the …
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory