Lesson 3

Mastering Python Sets: Uniqueness and Efficiency

Introduction

I'm delighted to welcome you to our Python Sets lesson! Remember, sets are like lists or tuples, except they can only hold unique elements. They're especially useful when you need to guarantee that elements in a collection appear only once.

In this lesson, you'll consolidate your knowledge of creating and operating on sets. You will learn about the special frozen sets and discover how sets enhance performance. Ready, Set, Go!

Creating and Manipulating Sets

Let's begin by creating a set in Python. It can be done either by the set() function or with {} syntax.

Python
1# Creating a set and printing it 2my_set = {1, 2, 3, 4, 5, 5, 5} # Duplicates will be omitted 3my_set_2 = set([1, 2, 3, 4, 5, 5, 5]) # The same set 4print(my_set) # Output: {1, 2, 3, 4, 5} 5print(my_set_2) # Output: {1, 2, 3, 4, 5}

Python provides methods to manipulate sets, such as add(), in, remove(), and discard().

Python
1# Adding an element 2my_set.add(6) # my_set is now {1, 2, 3, 4, 5, 6} 3 4print(1 in my_set) # Output: True, as `my_set` includes an element 1 5 6# Removing an element 7my_set.remove(1) # my_set becomes {2, 3, 4, 5, 6} 8 9print(1 in my_set) # Output: False, as `my_set` doesn't include 1 anymore 10 11# Discarding an element 12my_set.discard(7) # No changes - 7 doesn't exist in my_set

Both remove() and discard() methods are used for removing elements from a set, but they behave slightly differently:

  • remove(): Removes a specified element from the set. If the element is not found, it raises a KeyError.
  • discard(): Also removes a specified element from the set. However, if the element is not found, it does nothing, and no error is raised.
Set Operations

Python provides built-in operations such as union, intersection, and difference for sets.

Python
1set_1 = {1, 2, 3, 4} # First set 2set_2 = {3, 4, 5, 6} # Second set 3 4# Set union 5print(set_1.union(set_2)) 6# Output: {1, 2, 3, 4, 5, 6} 7 8# Set intersection 9print(set_1.intersection(set_2)) 10# Output: {3, 4} 11 12# Set difference 13print(set_1.difference(set_2)) 14# Output: {1, 2}
  • union(): Combines elements from both sets, excluding any duplicates. In this case, the result is a set containing {1, 2, 3, 4, 5, 6}.
  • intersection(): Returns a set with only the elements that are common to both sets. For these sets, the intersection is {3, 4}.
  • difference(): Returns a set containing elements that are in the first set but not in the second set. Here, the result is {1, 2} for set_1.difference(set_2).
Frozen Sets

A frozenset is an immutable set that cannot be modified once declared. It is similar to an immutable version of sets, much like the function of tuples in the family of sets.

Python
1my_fset = frozenset([1, 2, 3, 4, 5, 5, 5]) 2print(my_fset) # Output: frozenset({1, 2, 3, 4, 5})

Frozen sets are handy when you wish to use a set as a dictionary key but can't use regular sets.

Performance Benefits of Sets

One of the key advantages of sets is their faster performance in membership tests, which results from their use of hash tables.

Python
1import time 2 3my_set = set(range(1000000)) # A set of 10^6 elements 4my_list = list(range(1000000)) # A list with the same elements and order 5 6# Time taken for the set search 7start = time.time() 8print(999999 in my_set) # Sets find the number swiftly 9print("Time taken for set: ", time.time() - start) 10# Output: 11# True 12# Time taken for set: 2.86102294921875e-05 13 14# Time taken for the list search 15start = time.time() 16print(999999 in my_list) # Lists take longer to find the number 17print("Time taken for list: ", time.time() - start) 18# Output: 19# True 20# Time taken for list: 0.10818290710449219
  • Membership Test with Set: Thanks to hash tables, sets can check for membership in constant time, leading to quick lookup times. The time taken for checking membership in the set is remarkably low, as shown by the Time taken for set.
  • Membership Test with List: Lists require a linear search to check for membership, which results in longer lookup times as the list grows. The time taken for checking membership in the list is noticeably higher, demonstrated by Time taken for list.
Lesson Summary

Congratulations! You've just explored creating and manipulating sets, performing set operations, using frozen sets, and reaping the performance benefits of sets.

Remember, practice is key to solidifying your understanding. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.