1 of 18

Sets and Tuples

Winter 2025

1

Adrian Salguero

2 of 18

Announcements

  • Midterms will be graded and submitted by this upcoming Wednesday (ideally)
  • Coding Practice 5 due Wednesday, February 19th at 11:59pm
  • Homework 4, Part 2 is due Friday, February 21st at 11:59pm
  • Fill out the Mid-Quarter Feedback Form, closes tonight at 11:59pm
    • 1 point extra credit for Midterm if you fill it out

2

3 of 18

Sets

  • Mathematical Set: a collection of values, without duplicates or order
  • Order does not matter
    • {1, 2, 3} == {3, 2, 1}
  • No duplicates
    • {3, 1, 4, 1, 5} is not possible as a set because we have a duplicate 1, instead as a set this will be {3, 1, 4, 5}

3

4 of 18

Creating a Set

  • Direct mathematical syntax
    • odd = {1, 3, 5}
    • prime = {2, 3, 5}
    • Why can we not declare an empty set like this? → empty = { }
  • Construct from a list, tuple, or string:
    • odd = set([1, 3, 5]) # from a list
    • prime = set((2, 3, 5)) # from a tuple
    • name = set("Adrian") # from a string
      • name = {'i', 'A', 'd', 'n', 'r', 'a'} (not in this order each time)

4

5 of 18

Set Operations

5

Mathematical Operation

In Python

Example

Membership

in

4 in prime → False

Union

|

odd | prime → {1, 2, 3, 5}

Intersection

&

odd & prime → {3, 5}

Difference

-

odd - prime → {1}

Symmetric difference

^

odd ^ prime → {1, 2}

odd = {1, 3, 5}

prime = {2, 3, 5}

6 of 18

Set Iterations

  • We can still iterate over sets in the traditional sense

for item in myset:

# do something with item

  • We cannot index on sets to access elements
    • If you need to access elements based on index position, use a list instead!

6

7 of 18

Modifying Sets

  • Add an element to a set
    • myset.add(element)
    • myset = myset | {element} # adding using set union
  • Remove an element from a set
    • myset.remove(element)
    • myset.discard(element)
    • myset = myset - {element}
    • What would this do? → myset = myset - element
  • Remove and return an arbitrary element from a set
    • myset.pop()

7

8 of 18

Practice with Sets

z = {5, 6, 7, 8}

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

k = z & y

j = z | y

m = y - z

n = z - y

8

9 of 18

More Practice with Sets

z = {5, 6, 7, 8}

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

k = z.intersection(y)

j = z.union(y)

m = y.difference(z)

n = z.difference(y)

9

10 of 18

Even More Practice with Sets

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}

10

11 of 18

List vs. Set Operations

Suppose we have two lists, list1 and list2, and we want to find the common elements in the lists.

out1 = []

for elem in list2:

if elem in list1:

out1.append(elem)

-----------------------------------------------------------------

Suppose we have two sets, set1 and set2, and we want to find the common elements in the sets.

set1 & set2

11

12 of 18

List vs. Set Operations (cont.)

Find elements in either list1 or list2 (or both) (without duplicates)

out2 = list(list1) # make a copy

for elem in list2:

if elem not in out2: # don’t append elements already in out2

out2.append(elem)

Another way:

out2 = list1 + list2 # if an item is in BOTH lists, it will appear TWICE!

for elem in out1: # out1 = common elements in both lists

out2.remove(elem) # Remove common elements, leaving just a single copy

-----------------------------------------------------------------

Find the elements in either set1 or set2 (or both)

set1 | set2

12

13 of 18

List vs Set Operations (cont.)

Find the elements in either list but not in both

out3 = []

out2 = list1 + list2 # if an item is in BOTH lists, it will appear TWICE!

for elem in out2:

if elem not in list1 or elem not in list2:

out3.append(elem)

-----------------------------------------------------------------

Find the elements in either set but not in both:

set1 ^ set2

13

14 of 18

Mutable vs. Immutable

  • Mutable means we can add or remove elements from it, we can change it without having to declare a new value of the same type
    • Lists, dictionaries, and sets
  • Immutable means we cannot change or remove elements from it, we have to declare a new value of the same type
    • int, float, bool, string, tuple

14

15 of 18

Tuples

  • Like lists, tuples represents an ordered sequence of values
  • Like strings, tuples are immutable
  • The elements of a tuple can be anything (including mutable types)

Examples:

()

(4, 7, 9)

("hi", [1, 2], 5)

15

16 of 18

Tuple operations

  • Creating a set

("Once", "upon", "a", "time", "there", "was", "a")

(3, 1) + (4, 1) => (3, 1, 4, 1) # creates a new tuple!

  • Accessing a set
    • Can index just like lists:

tup = ("Once", "upon", "a", "time", "there", "was", "a")

print(tup[0]) => "Once"

print(tup[-1]) => "a"

  • Tuples are immutable, once it is created it cannot be updated

16

17 of 18

Tuple operations

17

  • Creating a tuple

("Once", "upon", "a", "time", "there", "was", "a")

(3, 1) + (4, 1) => (3, 1, 4, 1) # a new tuple!

  • Accessing a tuple

tup = ("Once", "upon", "a", "time", "there", "was", "a")

print(tup[0]) => "Once"

print(tup[-1]) => "a"

  • Tuples are immutable!

18 of 18

Tuples are immutable�Lists are mutable

18

def update_record(record, position, value):

"""Change the value at the given position"""

record[position] = value

mylist = [1, 2, 3]

mytuple = (1, 2, 3)

update_record(mylist, 1, 10)

print(mylist)

update_record(mytuple, 1, 10)

print(mytuple)