1 of 15

CSE 163

Tuples, Sets, and Dictionaries!

Oh My!�

Suh Young Choi�

🎶 Listening to: C418

💬 Before Class: Do you have a favorite extinct creature/plant? Why not?

2 of 15

This Time

  • List Comprehensions
  • Tuples
  • Sets
  • Dictionaries
  • Type Annotations Revisited

Last Time

  • Lists
  • Files

2

3 of 15

Project Data Exploration

Find 2 datasets that you may want to explore for the final project and answer some questions about them.

This is not a commitment, just an exploration!

Project Data Exploration due on Gradescope at 11:59pm, Monday 7/7

Submit a PDF! (Other file types do not load in Gradescope, so we cannot grade them.)

3

4 of 15

Recap

Learned about 4 data structures so far!

  1. List� Has indices, this can be added/removed at any place
  2. Set � No duplicates, fast membership queries, no order
  3. Dictionary� Like a generic list that can have any value as keys� Keys are unique, values are not
  4. Tuple� Immutable list, used for multiple returns, can “unpack”

4

5 of 15

List Comprehensions

squares = [

5

]

for i in range(1, 11)

if i % 2 == 0

:

:

squares.append(i ** 2)

i ** 2

6 of 15

List Comprehensions

squares = [

6

squares.append(i ** 2)

]

i ** 2

for i in range(1, 11)

if i % 2 == 0

7 of 15

List Comprehensions

squares = [

7

squares.append(i ** 2)

]

i ** 2

for i in range(1, 11)

if i % 2 == 0

4) Put it all in a list

3) Expression with � loop variable

1) What are you looping � over?

2) [Optional] Skip if this �is false

8 of 15

List Comprehensions

squares = [

8

squares.append(i ** 2)

]

i ** 2

for i in range(1, 11)

if i % 2 == 0

squares = [

]

for i in range(1, 11)

if i % 2 == 0

:

:

squares.append(i ** 2)

i ** 2

expression 🡪 iteration 🡪 condition

iteration

condition

expression

9 of 15

Tuples

  • A tuple is a lot like a list, but is immutable! (can’t change it)
    • Uses parentheses as start/end

9

t = (1, "hello", 3.4)

print(t) # (1, 'hello', 3.4)

print(t[0]) # 1

t[1] = 'goodbye' # Error!

10 of 15

Unpacking�Unpacking

  • We can "unpack" tuples to get their contents
  • The number of variables MUST match the number of tuple elements!

10

t = (1, "hello", 3.4)

# Good!

a, b, c = t

# Not good!

a, b = t

a, b, c, d = t

11 of 15

Set

  • Data structure highly optimized for membership queries
    • if x in set” is very fast
  • Acts a lot like a list, BUT
    • Does not allow indexing operations
    • Does not allow duplicates�

Set Methods

11

set()

Makes an empty set

s.add(x)

Adds x to s

s.remove(x)

Removes x from s

s.clear()

Removes all values from s

  • Note: {} does not make a new set!

12 of 15

Dictionary

  • Python has a data structure called a dictionary that allows us to use arbitrary data as indices (keys)

  • Keys are unique, but values may not be!
  • Basically a “fancy” list, most syntax is familiar
  • Lesson 6 will show some more advanced usages of the dictionary

12

d = {}

d['a'] = 1

d['b'] = 2

print(d) # {'a': 1, 'b': 2}

13 of 15

That which we call a data structure…

  • …might have many different data types in it!

  • Our type annotations should show this:

  • Careful about when you use pipes ( | ) and commas ( , )!
    • list[str | int] means a list of strings, ints, or both
    • list[str, int] means a list of strings AND ints

  • Same with parameters and returns:

13

strings: list[str] = [‘by’, ‘any’, ‘other’, ‘name’]

numbers: dict[str, float] = {“Act 2”: 2.24}

def mystery(a: bool | int, b: int) -> set[str, float]:

14 of 15

Group Work:

Best Practices

When you first working with this group:

  • Introduce yourself!
  • If possible, angle one of your screens so that everyone can discuss together

Tips:

  • Starts with making sure everyone agrees to work on the same problem
  • Make sure everyone gets a chance to contribute!
  • Ask if everyone agrees and periodically ask each other questions!
  • Call TAs over for help if you need any!

14

15 of 15

Before Next Time

  • Complete Lesson 4
    • Remember not for points, but do go towards Checkpoint Tokens
  • CP 0 due tonight!
  • Start Lesson 5
  • Start on the Project Data Exploration!

Next Time

  • More dictionary methods
  • CSV data

15