1 of 14

CSE 160

Sets

Hannah Cheung

2 of 14

Announcements

  • Due Monday (7/25) @ 11:59 PM:
    • HW4 Part 1 Due
    • Machine learning (K-means clustering), Dictionaries
    • Submit programming + reflection survey
  • Due Wednesday (7/27) @ 11:59 PM:
    • HW4 Part 2 Due
    • Submit programming + reflection survey

2

3 of 14

Sets

  • A set is a data structure that contains values
    • Order does not matter
      • {1, 2, 3} == {3, 2, 1}

3

1

2

3

3

2

1

4 of 14

Sets

  • A set is a data structure that contains values
    • Order does not matter
    • No duplicates
      • {3, 1, 4, 1, 5} == {5, 4, 3, 1}

4

3

1

4

3

1

4

1

5

5

5 of 14

Sets

  • A set is a data structure that contains values
    • Order does not matter
    • No duplicates
    • For every data structure, ask:
      • How to create
      • How to query (look up) and perform other operations
      • How to modify

5

1

2

3

3

2

1

6 of 14

Set Creation

  • Creating an empty set
    • Do not use {} for empty set; it is empty dictionary

  • Creating a set with initial values

  • Creating a set from a list

6

s = set()

odd = {1, 3, 5}

prime = {2, 3, 5}

odd = set([1, 3, 5])

prime = set([2, 3, 5])

empty = set([]) # or set()

7 of 14

Set Operations

Iteration (but no indexing) over sets

7

odd = {1, 3, 5}

prime = {2, 3, 5}

4 in prime # membership

odd | prime # union

odd & prime # intersection

odd - prime # difference

for i in odd:

print(i)

8 of 14

Set Operations Practice

8

z = {5, 6, 7, 8}

y = {1, 2, 3, 1, 5}

k = z & y

j = z | y

m = y - z

n = z - y

9 of 14

Set Modification

  • Add element to set
    • my_set.add(element)
    • my_set = my_set | {element}
  • Remove element from set
    • my_set.remove(element)
      • Element must be in set or error occurs
    • my_set.discard(element)
      • No errors occur
    • my_set = my_set - {element}
    • my_set.pop()
      • Removes and returns arbitrary element

9

Note: add remove, and discard all return None

10 of 14

Set Modification Practice

10

z = {5, 6, 7, 8}

y = {1, 2, 3, 1, 5}

p = z

q = set(z) # makes a copy of set z

z.add(9)

q = q | {35}

z.discard(7)

q = q – {6, 1, 8}

11 of 14

List vs Set Operations

  • Find common elements in both list1 and list2

  • Find common elements in both set1 and set2

11

out = []

for elem in list2:

if elem in list1:

out.append(elem)

set1 & set2

12 of 14

List vs Set Operations

  • Find elements in either list1 and list2 (without duplicates)

  • Find elements in either set1 and set2

12

out = list(list1)

for elem in list2:

if elem not in list1:

out.append(elem)

set1 | set2

13 of 14

List vs Set Operations

  • Find elements in either list (but not both)

  • Find elements in either set (but not both)

13

out = []

out2 = list1 + list2

for elem in out2:

if elem not in list1 or elem not in list2:

out.append(elem)

set1 ^ set2

14 of 14

Set Limitations

  • Not every value may be placed in a set
    • Set elements must be immutable values
      • int, float, bool, string, tuple
      • not: list, set, dictionary
  • Set itself is mutable
    • We can add and remove elements
  • Aside: frozenset must contain immutable values and is immutable (cannot add or remove elements)

14