# Python Dictionary Exercises

Let’s check out some exercises that will help understand dictionaries better.

#### Exercise 8-a: Python Dictionary Value by Key

Dictionaries don't have index orders, so speaking about them regarding their first item or last item is not very correct. Next time you print a dictionary it may have a different order than you saw before. Instead they have keys, and we can use keys to call their values.

When was Plato born?

dict[“born”]

Correct syntax to call a dictionary’s value is its key inside brackets next to its name.

`answer_1 = dict["born"]`

#### Exercise 8-b: Editing Python Dictionaries

Change Plato's birth year from B.C. 427 to B.C. 428.

You can just use -428 unless you’d like to use a string format such as: “B.C. 428”

In dictionaries later values will always override previous values for the same key.

If you type the new value for the key you’d like to change it will be replaced.

To address a specific key in dictionaries:
dict[“key”]

`dict["born"] = -428`

#### Exercise 8-c: Adding a List to Python Dictionary (Nested Data)

Dictionaries can have nested data too. Also, you can add a new key to a dictionary as they are mutable (changeable). Try to add the key "work" to dict with values shown below.

"work": ["Apology", "Phaedo", "Republic", "Symposium"]

All you have to do is call the dict with key “work” and assign the list to it.

`dict["work"] = ["Apology", "Phaedo", "Republic", "Symposium"]`

#### Exercise 8-d: Adding Up to the Value of a Key's Value (integer)

Add 2 inches to the son's height.

You can assign the key’s current value +2 to itself.

More hint?

a = a+2

`dict["son's height"] = dict["son's height"] + 2`

#### Exercise 8-e: Items method of dictionaries to get a list of tuples w/ key-value pairs

Using .items() method generate a list of tuples consisted of each key value pair.

.items() method of Python dictionaries combines each key-value pair inside a tuple and returns a list of tuples. List consists of as many tuples as key-value pairs.

`ans_1 = dict.items()`

#### Exercise 8-f: Get Method to call a dictionary key's value

Using .get() method print the value of "son's eyes".

.get() method takes two parameters, 1) value being looked up, 2) value to return if nothing comes up.

`ans_1 = dict.get("son's eye color")`

#### Exercise 8-g: Get Method of Dictionaries for Sophisticated Value Calling

.get() method can be used just to get the value of a key. But it has more tricks up its sleeve.

Try to look for key: "son's age" and if nothing comes up make the .get() return "2".

.get() method takes two parameters, 1) value being looked up, 2) value to return if nothing comes up.

`ans_1=dict.get("son's age", 2)`

#### Exercise 8-h: Clear method to remove everything from a dictionary

Since dictionaries are mutable, they have some methods that tuples don't have.

.clear() is one of them and it clears the whole dictionary.

.clear() method clear all keys from a dictionary.

`dict.clear()`

#### Exercise 8-i: Len function to get the amount of keys in a dictionary

Using len function, find how many keys there are in the dictionary.

len() is a function that works with multiple data types.

`ans_1 = len(dict)`

#### Exercise 8-j: Max function to get the key with the highest value

What's the key with the highest value in the dictionary?

max() function looks at the keys of the dictionary and returns the highest value.

`ans_1 = max(dict)`

#### Exercise 8-k: Min function to get the key with the lowest value

What's the key with the lowest value in the dictionary?

min() function looks at the keys of the dictionary and returns the lowest value.

`ans_1 = min(dict)`