CSE 163
CSVs and Lists of Dictionaries
��Hunter Schafer
This Time
Last Time
2
Dictionary Methods
3
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 |
Processing CSVs
4
Name,Salary
Madrona,3
Ken,1
Ryan,3
[
{'Name': 'Madrona', 'Salary': 3},
{'Name': 'Ken', 'Salary': 1},
{'Name': ‘Ryan’, 'Salary': 3}
]
Processing CSVs
5
# 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]
Group Work:
Best Practices
When you first working with this group:
Tips:
6