Dictionary Operations
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Below is a Python program, split up into several pieces. After each
program, submit the value that would be associated with the variable answer
. Note: the checker accepts valid python objects so if the answer is a string it must be wrapped in quotation marks.
If the program in question would cause an error, write error (no quotes!) in the box.
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = len(a)
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = list(a.keys())
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = a[thing]
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = a['thing']
Relevant Reading - looking up values
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = a['cat'][-1]
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = 'cola' in a
Relevant Reading - in operator
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = 'coca' in a
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = 'ola' in a['coca']
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = a.get('coca', 20)
Relevant Reading - get operator
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = a[42]
a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
answer = a.get('something', 27)
b = {'name': 'MIT',
'students': [{'name': 'Tim Beaver', 'id': 910284619},
{'name': 'Sally Kornbluth', 'id': 999999999}],
'location': {'state': 'MA', 'country': 'USA'}}
answer = b['location']['state'][0]
Next Exercise: Dictionary Swap