Sets

CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development
Adriana Picoral

Sets

  • A set is a mutable object
  • Contains nonduplicate elements
  • unordered datastructure
  • Type name set
  • literal uses curly braces ({})
  • Only contains immutable elements
  • We can use built-in functions like len(), min(), max(), sum()

Methods

  • .add(value) adds an value to the set
  • .remove(value) removes an value from the set (throws error if no matching value found)
  • .discard(value) removes an value from the set (no error if no matching value found)

Since items in a set are not ordered, not sequential, there’s no index in sets.

Sets

Items in a set are not ordered, and cannot repeat.

my_set = {"apple", "banana", "pear", "kiwi", "kiwi"}
print(my_set)
{'apple', 'banana', 'kiwi', 'pear'}
my_set.add("grape")
print(my_set)
{'pear', 'kiwi', 'banana', 'apple', 'grape'}
my_set.remove("banana")
print(my_set)
{'pear', 'kiwi', 'apple', 'grape'}

Set methods

  • .add(value) adds an item to the set, if the value is already there, nothing happens (it changes the set)
  • .union(other_set) returns a new set containing all unique elements from both sets – can also use |
  • .intersection(other_set) returns a new set containing only the elements common to both sets – can also use &
  • .difference(other_set) returns a new set containing elements present in one set but not in other_set – can also use -

Exercise

Two sections of a course each have a roster, stored as a list of student usernames. The lists may contain duplicates because of data-entry mistakes. Create a compare_roster.py file and write a function compare_rosters(section_a, section_b) that takes the two lists and returns a tuple of three values:

  1. A set of students enrolled in both sections
  2. A set of students enrolled only in section_a
  3. An integer: the total number of distinct students across both sections

Exercise

Requirements

  • You must use Python sets and set operations (&, |, -, or the matching methods .intersection(), .union(), .difference()).
  • Do not use loops to compare the lists.
  • Do not change the original lists.

Submit your compare_roster.py solution to gradescope

Exercise

Test cases:

assert compare_rosters(["ana", "ben", "cai", "ana"], ["ben", "dev", "cai"]) == ({"ben", "cai"}, {"ana"}, 4)
assert compare_rosters(["x", "y"], ["z"]) == (set(), {"x", "y"}, 3)
assert compare_rosters([], ["amy", "amy"]) == (set(), set(), 1)
assert compare_rosters([], []) == (set(), set(), 0)
assert compare_rosters(["p", "q"], ["q", "p"]) == ({"p", "q"}, set(), 2)
print("All tests passed!")

Submit your compare_roster.py solution to gradescope

Solution

def compare_rosters(section_a, section_b):
    set_a = set(section_a)
    set_b = set(section_b)

    students_in_both = set_a & set_b
    only_in_a = set_a - set_b
    total_unique = len(set_a | set_b)

    return students_in_both, only_in_a, total_unique

Why sets?

  • Python sets are implemented using hash tables
  • Faster membership check, faster insertion, faster deletion