Pybites Logo

Query a nested data structure

Level: Beginner (score: 2)

Given the provided cars dictionary:

  1. Get all Jeeps
  2. Get the first car of every manufacturer.
  3. Get all vehicles containing the string Trail in their names (should work for other grep too)
  4. Sort the models (values) alphabetically

See the docstrings and tests for more details. Have fun!

Update 18th of Sept 2018: as concluded in the forum it is better to pass the cars dict into each function to make its scope local.

Update 9th of March 2022: doing this exercise again ourselves today, we concluded that adding type hints would be quite beneficial here so we added them.

Update 27th of January 2025: the global scope of the cars dict was still causing issues (see here, note there are spoilers!), so removing default arguments + using deepcopy in the tests to work with new copies for each test.