Pre-Tutorial Exercises

If you've arrived early for the tutorial, please feel free to attempt the following exercises to warm-up.

In [1]:
# 1. Basic Python data structures
# I have a list of dictionaries as such:

names = [{'name': 'Eric',
          'surname': 'Ma'},
         {'name': 'Jeffrey',
          'surname': 'Elmer'},
         {'name': 'Mike',
          'surname': 'Lee'},
         {'name': 'Jennifer',
          'surname': 'Elmer'}]

# Write a function that takes in a list of dictionaries and a query surname, 
# and searches it for all individuals with a given surname.

def find_persons_with_surname(persons, query_surname):
    # Assert that the persons parameter is a list. 
    # This is a good defensive programming practice.
    assert isinstance(persons, list)   
    
    results = []
    for ______ in ______:
        if ___________ == __________:
            results.append(________)
    
    return results
In [2]:
# Test your result below.
results = find_persons_with_surname(names, 'Lee')
assert len(results) == 1

results = find_persons_with_surname(names, 'Elmer')
assert len(results) == 2
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-2-d7bd54c0478d> in <module>()
      1 # Test your result below.
----> 2 results = find_persons_with_surname(names, 'Lee')
      3 assert len(results) == 1
      4 
      5 results = find_persons_with_surname(names, 'Elmer')

<ipython-input-1-5332cbff266c> in find_persons_with_surname(persons, query_surname)
     20 
     21     results = []
---> 22     for ______ in ______:
     23         if ___________ == __________:
     24             results.append(________)

UnboundLocalError: local variable '______' referenced before assignment
In [ ]: