## ![](https://s3.amazonaws.com/python-ga/images/GA_Cog_Medium_White_RGB.png) {.separator}

Python Programming: Dictionaries

--- ## Learning Objectives *After this lesson, you will be able to:* - Perform common dictionary actions. - Build more complex dictionaries. --- ## Kicking Off Unit 3 In Unit 2, we ended by changing what our movie app printed depending on the value of a variable. Unit 3 is about **objects** in programming. - Objects are different kinds of things variables can hold. - Objects help give our programs more structure and functionality. - You already have one object down! Lists are an object with built-in functionality like `append()` and `pop()`. In Unit 3, we're going to add many more objects. By the end, your movie app will have the same functionality, but it will be structured in a totally different way. Ready? Let's go! --- ## Introducing Dictionaries Think about dictionaries — they're filled with words and definitions that are paired together. Programming has a dictionary object just like this! - Dictionaries hold keys (words) and values (the definitions). - In a real dictionary, you can look up a word and find the definition. In a Python dictionary, you can look up a key and find the value. --- ## Introducing Dictionaries ![](https://s3.amazonaws.com/ga-instruction/assets/python-fundamentals/dictionary.png) --- ## Declaring a Dictionary Dictionaries in programming are made of **key-value pairs**. ```python # Here's the syntax: name_of_dictionary = {"Key1": "Value", "Key2": "Value", "Key3": "Value"} print(name_of_dictionary[key_to_look_up]) # Prints the value # And in action... my_dictionary = {"Puppy": "Furry, energetic animal", "Pineapple": "Acidic tropical fruit", "Tea": "Herb-infused drink"} print(my_dictionary) # Prints the whole dictionary print(my_dictionary["Puppy"]) # => Prints Puppy's value: "Furry, energetic animal" ``` --- ## We Do: Dictionaries and Quick Tips The order of keys you see printed may differ from how you entered them. That's fine! You can't have the same key twice. Imagine having two "puppies" in a real dictionary! If you try, the last value will be the one that's kept. What's more, printing a key that doesn't exist gives an error. Let's create a dictionary together. --- ## We Do: Dictionary Syntax What if a value changes? We can reassign a key's value: `my_dictionary["Puppy"] = "Cheerful"`. What if we have new things to add? It's the same syntax as changing the value, just with a new key: `my_dictionary["Yoga"] = "Peaceful"`. - Changing values is case sensitive — be careful not to add a new key! --- ## Quick Review: Dictionaries We can: - Make a dictionary. - Print a dictionary. - Print one key's value. - Change a key's value. Here's a best practice: Declare your dictionary across multiple lines for readability. Which is better? ```python # This works but is not proper style. my_dictionary = {"Puppy": "Furry, energetic animal", "Pineapple": "Acidic tropical fruit", "Tea": "Herb-infused drink"} # Do this instead! my_dictionary = { "Puppy": "Furry, energetic animal", "Pineapple": "Acidic tropical fruit", "Tea": "Herb-infused drink" } ``` --- ## Discussion: Collection Identification Practice What are `a` and `b` below?: ```python # What object is this? collection_1 = [3, 5, 7, "nine"] # What object is this? collection_2 = {"three": 3, "five": 5, 9: "nine"} ``` --- ## Looping Through Dictionaries We can print a dictionary with `print(my_dictionary)`, but, like a list, we can also loop through the items with a `for` loop: --- ## Partner Exercise: Dictionary Practice You know the drill: Grab a partner and pick a driver! Create a new local file, `dictionary_practice.py`. Write a script that declares a dictionary called `my_name`. - Add a key for each letter in your name with a value of how many times that letter appears. As an example, here is the dictionary you'd make for `"Callee"`: ```python my_name = {"c": 1, "a": 1, "l": 2, "e": 2} ``` Write a loop that prints the dictionary, but formatted. ```python # The letter l appears in my name 2 times. ``` **Bonus (if you have time)**: If it's only one time, instead print `The letter l appears in my name once`. If it's only two times, instead print `The letter l appears in my name twice.` --- ## Partner Exercise: Most Popular Word With the same partner, switch who's driving. Write a function, `max_val()`, that takes a dictionary and returns the maximum value of any of the keys in the dictionary. Assume that the input dictionary's values will always be non-null integers. For example: ```python input_dict = { 'a': 2, 'b': 3, 'c': 1 } max_val(input_dict) # returns 3 ``` **Hints:** - When looping over a dictionary, does Python loop over the keys or the values? How do you make it loop over values? - Bonus: have the function return the maximum value, _and_ the key associated with that value --- ## Other Values We're almost there! Let's make this more complex. In a list or a dictionary, anything can be a value. Open this repl.it in a new window so you can see it all (the button in the top-right corner). --- ## Summary and Q&A Dictionaries: - Are another kind of collection, instead of a list. - Use **keys** to access **values**, not indices! - Should be used instead of lists when: - You don't care about the order of the items. - You'd prefer more meaningful keys than just index numbers. ```python my_dictionary = { "Puppy": "Furry, energetic animal", "Pineapple": "Acidic tropical fruit", "Tea": "Herb-infused drink" } ```