# create a dictionary with one key:value pair
animals = {'dog':'canine'}

print(animals)

"""
Explanation: Dictionary tutorial
Python has several useful data types beyond what we've already been using in class.  One that is particularly helpful in some circumstances is the dictionary.  In some ways, a dictionary behaves like a list - however, instead of accessing elements of the list by a number (e.g., my_list[3]), you access values by using keys, which can be strings or numbers.  As you might imagine, using strings as keys is particularly helpful!
A pair of braces are used to create an empty dictionary: {}
Placing a comma-separated list of key:value pairs within the braces adds initial pairs to the dictionary:
End of explanation
"""


animals['rabbit'] = 'rodent'
animals['lion'] = 'cat'
animals['mouse'] = 'rodent'
animals['coyote'] = 'canine'

print(animals)

print(animals['mouse'])

"""
Explanation: New key:value pairs can be added easily:
End of explanation
"""


mouse_type = animals['mouse']

print("the mouse type is:", mouse_type)

"""
Explanation: You can get at the value for a particular key:
End of explanation
"""


num_entries = len(animals)
print("there are", num_entries, "entries")

"""
Explanation: You can find out how many key:value pairs there are in the dictionary:
End of explanation
"""


what_is_a_rabbit = animals['rabbit']

print(what_is_a_rabbit)

"""
Explanation: You can get the value of a particular key and store it in a variable:
End of explanation
"""


animals['rabbit'] = 'NOT A CANINE'

print(animals)

"""
Explanation: Values associated with keys can be modified:
End of explanation
"""


del animals['rabbit']

print(animals)

"""
Explanation: Pairs that exist can be deleted:
End of explanation
"""


# print out the existing keys
animals.keys()

"""
Explanation: You can print out the keys that exist:
End of explanation
"""


# get a list of the keys that exist
list_of_animal_keys = list(animals.keys())

list_of_animal_values = list(animals.values())

print("list of keys:", list_of_animal_keys)
print("list of values:", list_of_animal_values)

"""
Explanation: You can also make a list of the keys that exist, and also a list of the values:
End of explanation
"""


for key in animals.keys():
    print(key)

"""
Explanation: You can also loop over the existing keys in the dictionary:
End of explanation
"""


for key, val in animals.items():
    print("key is:", key, "\tand value is:", val)

"""
Explanation: You can loop over the dictionary and access both the keys and the values:
End of explanation
"""


if 'lion' in animals:
    print("lion is one of the keys, and a lion is a", animals['lion'])
    
if 'donkey' not in animals:
    print("donkey is not one of the keys.  BOO!")

"""
Explanation: You can check to see if a key either is or is not in the dictionary:
End of explanation
"""


# make a dictionary containing some of the types and setting the number to zero.  
# Note that we are deliberately leaving one out!
number_each_animal_type = {'rodent':0, 'canine':0}

print("our starting dict of animal types:", number_each_animal_type)

# now, we loop over our dictionary of animals
for key, val in animals.items():

    # if that animal's type exists in our dictionary of animal types, 
    # just add one to the number.
    if val in number_each_animal_type:
        number_each_animal_type[val] += 1

    # if that animal's type does NOT exist in our dictionary of animal types,
    # create it and then set it to one since this is the first one we've found.
    else:
        number_each_animal_type[val] = 1

print("animals:", animals)
print("number of each type", number_each_animal_type)

"""
Explanation: Here's a more complicated example of something neat we can do.  We can create a dictionary to keep track of how often something occurs - say, the number of rodents, canines, and cats that we have in our dictionary of animals.  We can then loop through our dictionary of animals and keep track of the number of times that each value appears in our dictionary.  If we have forgotten to put in one of our types, we can also add it on the fly!
End of explanation
"""


