1 of 15

CSE 163

CSVs and Lists of Dictionaries

Suh Young Choi�

🎶 Listening to: Tom Day

💬 Before Class: Before was was was, was was is. Ruminate!

2 of 15

This Time

  • Dictionary Methods
    • How to loop over dictionary
  • CSVs
  • List of Dictionaries

Last Time

  • List Comprehensions
  • Tuple
  • Sets
  • Dictionaries

2

3 of 15

Recap

Dictionaries

  • Key-value pairs
  • Non-indexable, but still searchable

CSV files

  • Comma-separated values
  • Parsed as a list of dictionaries

3

4 of 15

Dictionary Methods

  • Dictionary Methods

4

dict() or {}

Makes an empty dictionary

d[key]

Gets the value for k, KeyError if None

d[key] = val

Assigns val as the value for key

d.pop(key)

Removes key from this dictionary

d.keys()

Returns a collection of the keys

d.values()

Returns a collection of the values

d.items()

Returns a collection of (key, value) tuples

5 of 15

Processing CSVs

  • Well structured so they are easy to parse!

  • Not clear how to represent since we need info on rows and cols. Have to use multiple data structures!

  • We will use a list of dictionaries to store this information

5

6 of 15

Processing CSVs

6

Name,Salary

Madrona,3

Ken,1

Ryan,3

Name

Salary

Madrona

3

[

{'Name': 'Madrona', 'Salary': 3},

{‘Name': 'Ken', 'Salary': 3},

{‘Name': 'Ryan’, 'Salary’: 3}

]

7 of 15

Processing CSVs

7

[

{'Name': 'Madrona', 'Salary': 3},

{‘Name': 'Ken', 'Salary': 3},

{‘Name': 'Ryan’, 'Salary’: 3}

]

Name,Salary

Madrona,3

Ken,1

Ryan,3

Name

Salary

Madrona

3

8 of 15

Processing CSVs

8

Name,Salary

Madrona,3

Ken,1

Ryan,3

[

{'Name': 'Madrona', 'Salary': 3},

{‘Name': 'Ken', 'Salary': 3},

{‘Name': 'Ryan’, 'Salary’: 3}

]

Name

Salary

Madrona

3

Name

Ken

Salary

1

9 of 15

Processing CSVs

9

Name,Salary

Madrona,3

Ken,1

Ryan,3

[

{'Name': 'Madrona', 'Salary': 3},

{‘Name': 'Ken', 'Salary': 3},

{‘Name': 'Ryan’, 'Salary’: 3}

]

Name

Salary

Madrona

3

Name

Ken

Salary

1

10 of 15

Processing CSVs

10

Name,Salary

Madrona,3

Ken,1

Ryan,3

[

{'Name': 'Madrona', 'Salary': 3},

{‘Name': 'Ken', 'Salary': 3},

{‘Name': 'Ryan’, 'Salary’: 3}

]

Name

Salary

Madrona

3

Name

Ken

Salary

1

Salary

Name

Ryan

3

11 of 15

Processing CSVs

11

Name,Salary

Madrona,3

Ken,1

Ryan,3

[

{'Name': 'Madrona', 'Salary': 3},

{‘Name': 'Ken', 'Salary': 3},

{‘Name': 'Ryan’, 'Salary’: 3}

]

Name

Salary

Madrona

3

Name

Ken

Salary

1

Salary

Name

Ryan

3

12 of 15

Processing CSVs

12

Name,Salary

Madrona,3

Ken,1

Ryan,3

[

{'Name': 'Madrona', 'Salary': 3},

{‘Name': 'Ken', 'Salary': 3},

{‘Name': 'Ryan’, 'Salary’: 3}

]

Name

Salary

Madrona

3

Name

Ken

Salary

1

Salary

Name

Ryan

3

0

1

2

0

1

2

13 of 15

Lists have a type…

  • Be careful of the types!

13

# data is a list containing dictionaries

tas = cse163_utils.parse('tas.csv')

# tas[0] is a dictionary

madrona = tas[0]

# madrona['Salary'] is an int

madrona['Salary']

# Also works without an intermediate variable

tas[0]['Salary']

# Doesn't work! List doesn't allow string indices

tas['Salary'][0]

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 6
    • Remember not for points, but do go towards Checkpoint Tokens
  • Complete Data Exploration
  • Checkpoint 1, THA1, and LR1 released on Friday!

Next Time

  • CSV Data in DataFrames

15