13. Sets in Python

Quiz: 0/5

A set is a collection in Python that stores unique items. Unlike lists, sets don’t keep track of the order of elements, and they automatically remove any duplicates. So, if you add the same item to a set more than once, only one copy will stay!

Creating Sets

You can create a set by putting items inside curly braces {} or by using the set() function.

# Using curly braces
my_set = {1, 2, 3, 4}

# Using set() function (to create an empty set)
empty_set = set()

Adding and removing elements

You can add elements to a set using the add() method and remove elements with the remove() method.

# Adding an element
my_set.add(5)

# Removing an element
my_set.remove(3)

If you try to remove an element that doesn’t exist in the set, it will give an error. To avoid that, you can use discard() instead, which won’t cause an error if the item is not found.

my_set.discard(10)  # Won't give an error even if 10 is not in the set

Set operations

Sets have some useful operations that let you combine or compare sets:

  1. Union: | or union(): Combines two sets, keeping all unique elements from both.

    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    union_set = set1 | set2  # Or you can use set1.union(set2)
    # Result: {1, 2, 3, 4, 5}
    
  2. Intersection: & or intersection(): Returns only the items that appear in both sets.

    intersection_set = set1 & set2  # Or you can use set1.intersection(set2)
    # Result: {3}
    
  3. Difference: - or difference(): Returns items in the first set but not in the second.

    difference_set = set1 - set2  # Or you can use set1.difference(set2)
    # Result: {1, 2}
    

Removing duplicates from Lists

Since sets only store unique elements, you can use a set to remove duplicates from a list. Simply convert the list to a set and then back to a list.

my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(my_list))
# Result: [1, 2, 3, 4, 5] (order is not guaranteed)

Summary

  • Sets store unique elements and don't care about the order.
  • You can add and remove items, but there’s no index like in lists.
  • Set operations like union, intersection, and difference let you work with multiple sets.
  • You can remove duplicates from a list by converting it to a set.

Hands-On Practise

Exercise: Movie collection organizer

You are organizing your movie collection, and you need to perform a few tasks using sets. Your collection includes movies that belong to different genres, and sometimes there are duplicates (e.g., you have multiple copies of the same movie).

Task 1: Remove duplicate movies

You have a list of movies, but there are duplicates. Convert the list to a set to remove any duplicates and then convert it back to a list.

movie_collection = ["Inception", "The Dark Knight", "Inception", "Interstellar", "The Matrix", "The Dark Knight"]

Task 2: Find movies in both action and sci-fi genres

You have two sets: one for Action movies and one for Sci-Fi movies. Find the movies that are in both genres using set intersection.

action_movies = {"The Dark Knight", "Die Hard", "Mad Max: Fury Road", "John Wick"}
scifi_movies = {"The Matrix", "Inception", "Interstellar", "Mad Max: Fury Road"}

Task 3: Combine movies from two different genres

You have two sets: one for Action movies and one for Comedy movies. Combine all movies from both genres into one set.

comedy_movies = {"The Hangover", "Superbad", "Step Brothers", "The Matrix"}
action_movies = {"Die Hard", "Mad Max: Fury Road", "John Wick", "The Matrix"}

Challenge bonus:

Can you figure out how to find movies that are in Action but not in Comedy using set difference?

Output:

Quizzes: 0/5

Question 1:

What is a set in Python?

  • An ordered collection of unique items.
  • An unordered collection of unique items.
  • A collection of items that can contain duplicates.
  • An ordered collection of items that allows duplicates.

Question 2:

Which method is used to add an element to a set?

  • add_element()
  • insert()
  • add()
  • append()

Question 3:

What will happen if you try to add a duplicate element to a set?

  • It will raise an error.
  • The duplicate element will be added.
  • Only one copy of the element will remain in the set.
  • The set will automatically sort the elements.

Question 4:

Which set operation returns elements that are in both sets?

  • Union
  • Intersection
  • Difference
  • Symmetric Difference

Question 5:

How can you remove duplicates from a list using sets?

  • Convert the list to a set and then back to a list.
  • Use the `remove()` method.
  • Iterate over the list and remove duplicates manually.
  • Use the `discard()` method.

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonDictionaries in PythonNext lessonConversions between data structures