Sets
Lesson 1 of 2
  1. 1
    In 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…
  2. 2
    In 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 …
  3. 3
    Unlike 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…
  4. 4
    There 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…
  5. 5
    There 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. …
  6. 6
    In 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…
  7. 7
    A 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 …
  8. 8
    When working with set or frozenset container, one of the most common operations we can perform is a merge. To do this, we can return the union of two sets using the .union() method or | operator. D…
  9. 9
    Let’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…
  10. 10
    Similar 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…
  11. 11
    The 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…
  12. 12
    Great 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