\chapter{Dictionaries}\label{dictionaries} In this chapter we will work with Python dictionaries. Dictionaries are data structures that indexes values by a given key (key-value pairs). The following example shows a dictionary that indexes students ages by name. \begin{lstlisting} ages = { "Peter": 10, "Isabel": 11, "Anna": 9, "Thomas": 10, "Bob": 10, "Joseph": 11, "Maria": 12, "Gabriel": 10, } >>> print(ages["Peter"]) 10 \end{lstlisting} It is possible to iterate over the contents of a dictionary using "items", like this: \begin{lstlisting} >>> for name, age in ages.items(): ... print(name, age) ... Peter 10 Isabel 11 Anna 9 Thomas 10 Bob 10 Joseph 11 Maria 12 Gabriel 10 \end{lstlisting} However, keys don't need to be necessarily strings and integers but can be any objects: \begin{lstlisting} d = { 0: [0, 0, 0], 1: [1, 1, 1], 2: [2, 2, 2], } >>> d[2] [2, 2, 2] \end{lstlisting} Even more, you can use other dictionaries as values: \begin{lstlisting} students = { "Peter": {"age": 10, "address": "Lisbon"}, "Isabel": {"age": 11, "address": "Sesimbra"}, "Anna": {"age": 9, "address": "Lisbon"}, } >>> students['Peter'] {'age': 10, 'address': 'Lisbon'} >>> students['Peter']['address'] 'Lisbon' \end{lstlisting} This is quite useful to structure hierarchical information. \section{Exercises with dictionaries} Use the Python documentation at \url{https://docs.python.org/3/library/stdtypes.html#mapping-types-dict} to solve the following exercises. Take the following Python dictionary: \begin{verbatim} ages = { "Peter": 10, "Isabel": 11, "Anna": 9, "Thomas": 10, "Bob": 10, "Joseph": 11, "Maria": 12, "Gabriel": 10, } \end{verbatim} \begin{enumerate} \item How many students are in the dictionary? Search for the "len" function. \item Implement a function that receives the "ages" dictionary as parameter and return the average age of the students. Traverse all items on the dictionary using the "items" method as above. \item Implement a function that receives the "ages" dictionary as parameter and returns the name of the oldest student. \item Implement a function that receives the "ages" dictionary and a number "n" and returns a new dict where each student is $n$ years older. For instance, \textit{new\_ages(ages, 10)} returns a copy of "ages" where each student is 10 years older. \end{enumerate} \section{Exercises with sub-dictionaries} Take the following dictionary: \begin{verbatim} students = { "Peter": {"age": 10, "address": "Lisbon"}, "Isabel": {"age": 11, "address": "Sesimbra"}, "Anna": {"age": 9, "address": "Lisbon"}, } \end{verbatim} \begin{enumerate} \item How many students are in the "students" dict? Use the appropriate function. \item Implement a function that receives the students dict and returns the average age. \item Implement a function that receives the students dict and an address, and returns a list with the name of all students which address matches the address in the argument. For instance, invoking "find\_students(students, 'Lisbon')" should return Peter and Anna. \end{enumerate}