<jupyter_start><jupyter_text>Notes - Python Dictionaries  Python Collections (Arrays)There are four collection data types in the Python programming language:* **List** is a collection which is ordered and changeable. Allows duplicate members.* **Tuple** is a collection which is ordered and **unchangeable**. Allows duplicate members.* **Set** is a collection which is unordered and **unindexed**. **No duplicate** members.* **Dictionary** is a collection which is unordered and changeable. **No duplicate** members.  Python Dictionaries* a collection which is unordered, changeable and does not allow duplicates* stores data values in key:value pairs* Dictionaries are written with curly brackets, and have keys and values* Refer to items by keys, rather than by index  Creating Dictionaries<jupyter_code>Zoey = {"animal": "dog", "breed": "Yorkie", "gender": "female", "birth_year": 2008, "weight_pounds": 5.1, "nicknames":["Zo-zo", "Zamboey","Isabella"]}
Harley = {"animal": "dog", "breed": "Labrador", "gender": "male", "birth_year": 2020, "weight_pounds": 62.5, "nicknames":["Harley-Barley", "Barley-Boo","Har-Har"]}
Mochi = {"animal": "cat", "gender": "male", "birth_year": 2019, "weight_pounds": 7, "nicknames":["Mo-mo", "T",]}
Cade = {"animal": "gecko", "breed": "spotted leopard", "gender": "male", "birth_year": 2018}

print(Zoey)
print(Harley)
print(Mochi)
print(Cade)

#Note data types:
print(type(Zoey)) #dictionary
print(type(Zoey["breed"])) # string
print(type(Zoey["birth_year"])) # integer
print(type(Zoey["weight_pounds"])) # float
print(type(Zoey["nicknames"]))# list<jupyter_output>{'animal': 'dog', 'breed': 'Yorkie', 'gender': 'female', 'birth_year': 2008, 'weight_pounds': 5.1, 'nicknames': ['Zo-zo', 'Zamboey', 'Isabella']}
{'animal': 'dog', 'breed': 'Labrador', 'gender': 'male', 'birth_year': 2020, 'weight_pounds': 62.5, 'nicknames': ['Harley-Barley', 'Barley-Boo', 'Har-Har']}
{'animal': 'cat', 'gender': 'male', 'birth_year': 2019, 'weight_pounds': 7, 'nicknames': ['Mo-mo', 'T']}
{'animal': 'gecko', 'breed': 'spotted leopard', 'gender': 'male', 'birth_year': 2018}
<class 'dict'>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'list'><jupyter_text>Items* Items are unordered, changeable, and do not allow duplicates.* Items are presented in key:value pairs, and can be referred to by using the key name.<jupyter_code># Examples of referring to a key:value pair

print(Cade["breed"])
print(Zoey["breed"])
print(Harley["gender"])
print(Mochi["birth_year"])<jupyter_output>spotted leopard
Yorkie
male
2019<jupyter_text>* A duplicate item will overwrite itself.<jupyter_code># Example of overwriting an item
my_favorites = {"color":"blue", "number":14,"sport":"volleyball","color":"green"}
print(my_favorites)<jupyter_output>{'color': 'green', 'number': 14, 'sport': 'volleyball'}<jupyter_text>* To determine how many items a dictionary has, use the ```len()``` function:<jupyter_code>len(Zoey)
len(Cade)<jupyter_output><empty_output><jupyter_text>Accessing Individual Dictionary Items* You can access items of a dictionary by referring to its key name, inside square brackets:* You can use ```get``` to access items<jupyter_code># access using []

x = Cade["breed"]
print(x)


# access using get()

x = Cade.get("breed")
print(x)<jupyter_output>spotted leopard<jupyter_text>Accessing Dictionary Keys     * The ```keys()``` method will **return a list of all the keys**   in the dictionary.    * This is a good way to check if there have been changes to the dictionary.<jupyter_code>#Example of how to list the keys in a dictionary
Zoey.keys()<jupyter_output><empty_output><jupyter_text>Accessing Dictionary Values     * The ```values()``` method will **return a list of all the values**  in the dictionary.<jupyter_code>#Example of how to list the keys in a dictionary
Zoey.values()<jupyter_output><empty_output><jupyter_text>Accessing ALL Dictionary Items -- key:value     * The ```items()``` method will **return a list of all the items**  in the dictionary as a **tuples list**.     * This is a good way to check if there have been changes to the dictionary.<jupyter_code>#Example of how to list the keys in a dictionary
Zoey.items()<jupyter_output><empty_output><jupyter_text>To Check if a Key Exists    * To determine if a specified key is present in a dictionary use the ```in``` keyword<jupyter_code>if "nicknames" in Zoey:
  print("Yes, 'nicknames' is one of the keys in the thisdict dictionary")<jupyter_output>Yes, 'nicknames' is one of the keys in the thisdict dictionary<jupyter_text>To Change Values    * You can change the value of a specific item by referring to its key name:<jupyter_code>Cade["birth_year"] = 2017
print(Cade)<jupyter_output>{'animal': 'gecko', 'breed': 'spotted leopard', 'gender': 'male', 'birth_year': 2017}<jupyter_text>Updating Dictionaries    * The ```update()``` method will update the dictionary with the items from the given argument.* The argument must be a dictionary, or an iterable object with key:value pairs.<jupyter_code>Cade.update({"gender": "female"})
print(Cade)<jupyter_output>{'animal': 'gecko', 'breed': 'spotted leopard', 'gender': 'female', 'birth_year': 2017}<jupyter_text>Adding Items to Dictionaries    * Adding an item to the dictionary is done by using a new index key and assigning a value to it* The ```update()``` method will update the dictionary with the items from a given argument. **If the item does not exist, the item will be added.**  The argument must be a dictionary, or an iterable object with key:value pairs.<jupyter_code># Example of adding an item:

Cade["weight_pounds"] = 0.05
print(Cade)
# Example of adding an item with update():

Mochi.update({"breed": "mixed"})
print(Mochi)<jupyter_output>{'animal': 'cat', 'gender': 'male', 'birth_year': 2019, 'weight_pounds': 7, 'nicknames': ['Mo-mo', 'T'], 'breed': 'mixed'}<jupyter_text>Removing Items from Dictionaries     There are several methods to remove items from a dictionary:* The ```pop()``` method removes the item with the specified key name:<jupyter_code># using .pop to remove and item

Mochi.pop("breed")
print(Mochi)<jupyter_output>{'animal': 'cat', 'gender': 'male', 'birth_year': 2019, 'weight_pounds': 7, 'nicknames': ['Mo-mo', 'T']}<jupyter_text>* The ```popitem()``` method removes the last inserted item (in versions before 3.7, a random item is removed instead):<jupyter_code>Cade.popitem()
print(Cade)<jupyter_output>{'animal': 'gecko', 'breed': 'spotted leopard', 'gender': 'female', 'birth_year': 2017}<jupyter_text>* The ```del``` keyword removes the item with the specified key name.   BE CAREFUL not to delete the entire dictionary.<jupyter_code>Mochi = {"animal": "cat", "gender": "male", "birth_year": 2019, "weight_pounds": 7, "nicknames":["Mo-mo", "T",]}

del Mochi["gender"]
print(Mochi)<jupyter_output>{'animal': 'cat', 'birth_year': 2019, 'weight_pounds': 7, 'nicknames': ['Mo-mo', 'T']}<jupyter_text>----------------------------------------------------------------------                              !    !    !    W A R N I N G    !    !    !* The ```del``` keyword can also be used to delete the dictionary completely* The ```clear()``` method empties the dictionary----------------------------------------------------------------------   Loop Through a DictionaryYou can loop through a dictionary by using a for loop.When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.    * Print all **key names** in the dictionary, one by one:<jupyter_code># Example to print key names: 

for x in Zoey:
  print(x)<jupyter_output>animal
breed
gender
birth_year
weight_pounds
nicknames<jupyter_text>* Print all **values** in the dictionary, one by one:<jupyter_code>for x in Zoey:
  print(Zoey[x])<jupyter_output>dog
Yorkie
female
2008
5.1
['Zo-zo', 'Zamboey', 'Isabella']<jupyter_text>* You can also use the ```values()``` method to return values of a dictionary:<jupyter_code>for x in Zoey.values():
    print(x)<jupyter_output>dog
Yorkie
female
2008
5.1
['Zo-zo', 'Zamboey', 'Isabella']<jupyter_text>* You can use the ```keys()``` method to return the keys of a dictionary:<jupyter_code>for x in Zoey.keys():
    print(x)<jupyter_output>animal
breed
gender
birth_year
weight_pounds
nicknames<jupyter_text>* Loop through **both keys and values**, by using the ```items()``` method:<jupyter_code>for x, y in Zoey.items():
    print(x, y) 
    
# For readability try: print(x,':', y)<jupyter_output>animal dog
breed Yorkie
gender female
birth_year 2008
weight_pounds 5.1
nicknames ['Zo-zo', 'Zamboey', 'Isabella']<jupyter_text>Copy a Dictionary    You **cannot copy a dictionary simply by typing dict2 = dict1**, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2.* There are ways to make a copy, one way is to use the built-in Dictionary method ```copy()```.<jupyter_code># Example of copying a dictionary using copy()

new_zoey = Zoey.copy()
print(new_zoey)<jupyter_output>{'animal': 'dog', 'breed': 'Yorkie', 'gender': 'female', 'birth_year': 2008, 'weight_pounds': 5.1, 'nicknames': ['Zo-zo', 'Zamboey', 'Isabella']}<jupyter_text>* Another way to make a copy is to use the built-in function ```dict()```.<jupyter_code># Example of copying a dictionary using dict()

new_Mochi = dict(Mochi)
print(new_Mochi)<jupyter_output>{'animal': 'cat', 'birth_year': 2019, 'weight_pounds': 7, 'nicknames': ['Mo-mo', 'T']}<jupyter_text>Nested Dictionaries    A dictionary can contain dictionaries, this is called **nested dictionaries**.<jupyter_code># Example
# Create a dictionary that contain three dictionaries:

myfamily = {
  "child1" : {
    "name" : "Rachel",
    "year" : 2000
  },
  "child2" : {
    "name" : "Sarah",
    "year" : 2004
  },
  "child3" : {
    "name" : "Darrell",
    "year" : 1955
  }
}<jupyter_output><empty_output><jupyter_text>* Or, if you want to add three dictionaries into a new dictionary:<jupyter_code># Example
# Create three dictionaries, then create one dictionary that will contain the other three dictionaries:

child1 = {
  "name" : "Emil",
  "year" : 2004
}
child2 = {
  "name" : "Tobias",
  "year" : 2007
}
child3 = {
  "name" : "Linus",
  "year" : 2011
}

myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15967851263677738, 'fraction_numerical': 0.01588070107485233, 'mean_word_length': 5.340085942295887, 'pattern_counts': {'":': 32, '<': 84, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '599265', 'license': 'MIT', 'n_tokens_mistral': 3682, 'n_tokens_neox': 3366, 'n_words': 1138, 'path': 'study-notes-dictionaries.ipynb', 'repo_name': 'barbmarques/python-exercises', 'size': '10327'}