Another item from our wish list is an associative array, which is called a dictionary in Python.
This is a potentially confusing term, as it has a different meaning in computer science, not to mention real life.
It is like a dictionary in that you can look up information using a key.
The key is like a word and the value is like a definition.
A key should not be used for more than one entry, though a value can be reused.
Here's one way in which tuples are used: unlike lists, they can serve as keys, since keys must be immutable.
To create a dictionary, instead of using square brackets like for lists or parentheses like for tuples, we use curly brackets.
Separated by commas, we list key value pairs, with the key and value separated by a colon.
Once we've created a dictionary, we can use square brackets to specify a key, which results in a value being extracted.
Since dictionaries are mutable, we can also use the square brackets to specify a value that should be changed.
Notice that the order in which pairs are printed may not be what you expect.
Remember that although you list the pairs in some order, the order doesn't matter.
It is only by the association between keys and values that you can extract information.
There are methods to extract keys, as a list, or key-value pairs, as a list of tuples.
We can also use "in" and "not in" to determine whether or not a key exists in a dictionary.
This is handy, as we'll see shortly.
There's a special way of writing a "for" loop for a dictionary, which we'll demonstrate in an example.
Suppose we wish to write our own version of the "in" function in which we either return the value associated with a key or the string "Not found".
We can assign names to both the key and value in a particular tuple in the list of tuples of key-value pairs,
and use them separately in the loop body.
We use the key to compare to our input and the value as what we return.
If we make it all the way through the loop without returning a value, we return the string "Not found".
And now we can test our function, making sure to include both empty and nonempty dictionaries, and both possible outcomes.
Here are the functions we used to create a dictionary, access a value, or change a value.
And here are the other functions we used.
Notice that the focus is very much on the keys.
This is an indication of the type of situation in which to use a dictionary.
If your information is not structured around the keys, then a dictionary is not the right choice.
Here's an example of where a dictionary is very useful.
We'll use keys to represent distinct strings we encounter in a list and values to represent how many we've seen so far.
Then we'll figure out the maximum.
That allows us to process each string in turn, updating the dictionary values as we go.
We're going to assume that every string is a name of a colour, hence the name.
We use for to process each item in the list.
There are two situations, depending on whether or not the current string is already in the dictionary.
If it is, we just update the value, showing that we have seen that string yet again.
Otherwise we set the value to 1.
Since item is not a key, this creates a new key-value pair with the key item and the value 1.
We have to be sure to initialize the dictionary.
After all elements in the input have been processed,
we have a dictionary in which the names of the colours are the keys and the values are how many times they appear.
To figure out the maximum, we iterate through all key value pairs.
If the value is greater than the largest number we've seen so far,
we update the value of the largest number and the colour to return.
We initialize the largest value to 0 and the colour to the string "Empty" for the case of an empty list.
Finally, we return a colour that appears the maximum number of times.
In our tests, we consider both empty and non-empty lists as inputs.
Our last example will not use dictionaries,
but it will use a function that allows us to iterate over two sequences at the same time, just like we iterated over both keys and values.
We will once again look at a list of salaries and a list of boosters, with the goal being to calculate the new salaries.
Here we are using the zip function to join salaries and boosters.
This allows us to iterate over both.
We update our new list by concatenating a new element,
making sure that it has been initialized.
We return the output,
and test that our function works on both empty and non-empty pairs of lists.
You can keep this in mind for functions on two lists or any other types of sequences.