# 11 - Dictionaries

1. Which of the following options creates a dictionary? Note: Dictionaries are created by specifying keys and values. You can also create an empty dictionary.
```a) d = {}
b) d = {"john":40, "peter":45}
c) Both a and b create a dictionary
d) Neither a nor b apply ```

a

b

c

d

2. In the dictionary 'd' can you point out the keys?
`d = {"jonathan":33, "Ruth":35}`

33, 35

jonathan, Ruth

jonathan, 33, Ruth, 35

d is the only key

3. Dictionaries make searching incredibly easy. Compare this to using a list and searching using a loop! Predict the output for the following:
```mydictionary = {"jonathan":40, "peter":45}
"jonathan" in mydictionary```

True ("in" can be used to check if the key exists in the dictionary)

john

40

False ("in" can be used to check if the key exists, but in this case it doesn't)

4. Can you predict the output for the following:
```d1 = {"score1":40, "score2":45}
d2 = {"score1":466, "score2":85}
d1 == d2```

1

Error

False

True

5. The output here will be ____________ because d["score1"] is looking up the key 'score1' and will return the _____ stored in the dictionary for that key.
```d = {"score1":40, "score2":45}
d["score1"]```

40 / element

40 / key

40 / value

score1 / key

6. What does the following code do? (in relation to the dictionary myDict)
```myDict = {'a':1,'b':2,'c':3,'d':4}
print(myDict)
if 'a' in myDict:
del myDict['a']
print(myDict)```

It removes all the values from the dictionary

It deletes the entire dictionary

It duplicates the dictionary and then deletes the first copy

It removes a key from a dictionary

7. To obtain the number of entries in a dictionary called 'd', which command would be used?
```a) d.size()
b) len(d)
c) size(d)
d) d.len()```

c

a

b

d

8. Remember, dictionaries are perfect for when you have a key-value pair. What is the output here? #note that lists are defined in "[]"and tuples in "()". Tuples are considered immutable.
```d = {"john":40, "peter":45}
print(list(d.keys()))```

['peter','peter']

['john', 'peter']

['peter']

['john']

9. Simply put, a mutable object can be changed after it is created, and an immutable object cannot. Fill in the blanks below.
```Objects of built-in types like (int, float, bool, str, tuple, unicode)
are __________.
Objects of built-in types like (list, set, dict) are
_________.```

mutable / immutable

immutable /mutable

immutable / fixed

verifiable / mutable

10. Analyse the code below which uses a dictionary and the 'count' command. What is the output? Remember you can go back to the 'code library' on www.teachyourselfpython.com to find code snippets on dictionaries.
```def main():
dictFilms = {
'Group 1' : (90, 90),
'Group 2' : (44, 45),
'Group 3' : (47, 90),
'Group 4' : (90, 90),
'Group 5' : (25, 23)  }

my_list=[]
for key in dictFilms:
if dictFilms[key].count(90)>1:
my_list.append(key)

print("group(s) that have more than 1 occurence of a 90 score are:", my_list)

main()```

['Group 4', 'Group 5'])

['Group 3', 'Group 4'])

['Group 1', 'Group 2'])

['Group 1', 'Group 4'])