Previous Lecture lect15 Next Lecture

’’’ Dictionaries

D = {} # Empty dictionary. Notice the curly braces instead of []

D = {‘Jane’:18, ‘Jen’:19, ‘Joe’:20, ‘John’:21}

’’’ print(D) print(“Jane’s age is:”, D[‘Jane’]) print(len(D))

Simple way to get key / value

for x in D: print(x) # Prints the keys print(D[x]) # Prints the values ‘’’ ‘’’ Restrictions on using Dictionaries

’’’ dictionary methods D.pop(key) # remove and return the value D.update(D2) # combine values in D and D2 D.get(key) # returns item if it exists. If not, returns None or a default value D.keys() # dict_keys([ LIST OF KEYS ]) D.values() # dict_values([ LIST OF VALUES ]) D.items() # dict_items([ LIST OF KEY,VALUE ]) ‘’’ ‘’’ value = D.pop(‘John’) print(D) print(value) D.update({‘Jess’:25,’Jerry’:27}) print(D)

What if something is not in the dictionary and we try to access it?

#print(D[“CS8”]) #ERROR - “CS8” key doesn’t exist. print(D.get(“CS8”))

Can define the default return value if key doesn’t exist

print(D.get(“CS8”, “not in dictionary!”))

print(D.keys()) print(D.values()) ‘’’ ‘’’ for item in D.keys(): print(item) ‘’’ ‘’’ keys = D.keys() #print(keys[2]) # ERROR - Not a list! print(list(keys)[2]) # OK ‘’’

Example of adding to a dictionary

D = {} print(D) D[‘CS8’] = “UCSB” print(D) D[‘CS16’] = “UCSB” print(D) ‘’’

```