<jupyter_start><jupyter_text>[Dictionaries](http://www.learnpython.org/en/Dictionaries)There are other things like [NamedTuples](https://docs.python.org/3/library/collections.htmlcollections.namedtuple) or [DefaultDict](https://docs.python.org/3/library/collections.htmlcollections.defaultdict) (both from the [collections](https://docs.python.org/3/library/collections.html) library) you can use, however there are some great options when using dictionaries that you can't when using NamedTuples. (And DefaultDict limits your options for dictionary use - but it has it's purpose!) StructureThe **dictionary** documentation can be found [here](https://docs.python.org/3/tutorial/datastructures.htmldictionaries). It is a data set that is distinguished by the `{}` braces (similar to a set) but has a key and value pair.Each key **must** be unique. _(**NOTE:**  last assignment to a key wins!)_One of the things I love about programming is that there is no one right way to code.Here I will share with you 3 ways you can create a dictionary.  Create Dict Option 1Unlike sequences (like **strings**, **lists**, and **tuples**) indexed by a range of numbers? Items within a dictionary are indexed by keys.Try this!```pythonnew_dict = dict()new_dict['some_key'] = "Whatever you want here. Any data type. PEP8 says should all be the same, but that's a style guideline."new_dict[4] = 4```<jupyter_code>new_dict = dict()
new_dict['some_key'] = "Whatever you want here. Any data type. PEP8 says should all be the same, but that's a style guideline."
new_dict[4] = 4
print(new_dict)<jupyter_output><empty_output><jupyter_text>Create Dict Option 2Try this!```pythonnew_dict2 = {'sum things': ["Go Here", ]}print(new_dict2)```If there is more than one item in a dictionary, then they will be separated by a comma.<jupyter_code>new_dict2 = {'sum things': ["Go Here", ]}
print(new_dict2)<jupyter_output><empty_output><jupyter_text>Create Dict Option 3Another great way to create a dictionary is by using keyword arguments:Try this!```pythondict(sape=4139, guido=4127, jack=4098)```<jupyter_code>dict(sape=4139, guido=4127, jack=4098)<jupyter_output><empty_output><jupyter_text>Things To Keep In MindYou cannot use a list as a key. Keys must be immutable like **strings** and **numbers**.A dictionary is _kind of_ like a [set](https://docs.python.org/3/tutorial/datastructures.htmlsets) in that if you try to create multiple keys with the same value? It will only keep 1 key / value pair.The last one.Prior to 3.6 they were also unordered ... Rumor has it that [this was changed in 3.6 version](https://docs.python.org/3/whatsnew/3.6.html)! ;) Built-In MethodsBe sure to check out the structure of a **dictionary** [here](https://docs.python.org/3.3/library/stdtypes.htmldict)!When you utilize these documents, you provide yourself the ability to do more. How? Because you know what's already available and won't have to reinvent the wheel!  Dict ComprehensionsLike list comprehensions, this provides you the ability to create dictionaries from some arbitrary (seemingly random) key and value expressions. For example:Try this!```python{x: x**2 for x in (2, 4, 6)}```<jupyter_code>{x: x**2 for x in (2, 4, 6)}<jupyter_output><empty_output><jupyter_text>Try this!```pythontmp_tup = ('a','b','c'){tmp_tup[idx]: x for idx, x in enumerate((2, 4, 6))}```NOTE:  This exercise can also be done using zip() function but I wanted to provide another dict comprehension example.<jupyter_code>tmp_tup = ('a','b','c')
{tmp_tup[idx]: x for idx, x in enumerate((2, 4, 6))}<jupyter_output><empty_output><jupyter_text>Add & UpdateHere are some ways with which you can interact with a dictionary.Try this!```pythondict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}print(dict)dict['Age'] = 8	 						 update existing entryprint(dict)dict['School'] = "DPS School"			 Add new entryprint(dict)print("dict['Age']: ", dict['Age']) 	Prints value at key ‘Age’print(dict)								 Prints complete dictionaryprint(dict.keys())   					 Prints all the keysprint(dict.values()) 					 Prints all the values!```<jupyter_code>dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print(dict)
dict['Age'] = 8                         # update existing entry
print(dict)
dict['School'] = "DPS School"           # Add new entry
print(dict)
print("dict['Age']: ", dict['Age'])     #Prints value at key ‘Age’
print(dict)                             # Prints complete dictionary
print(dict.keys())                      # Prints all the keys
print(dict.values())                    # Prints all the values!<jupyter_output><empty_output><jupyter_text>DeletionHere are some ways to remove an item from a dictionary, thank you [TutorialsPoint](http://www.tutorialspoint.com/python/python_dictionary.htm).Try this!```pythontemp_dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}print(temp_dict)``````pythondel temp_dict['Name']	         remove entry with key 'Name'print(temp_dict)``````pythontemp_data = temp_dict.pop("Age")             returns entry and removes from dictionaryprint(temp_dict)``````pythondel temp_datatemp_dict.clear()				 remove all entries in dictprint(temp_dict)del temp_dict					 removes dict from local scope (doesn’t actually delete)```**QUESTION:**  What kind of [exception](https://docs.python.org/3/tutorial/errors.html) will be raised if you try to access your deleted dictionary?<jupyter_code>temp_dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print(temp_dict)
del temp_dict['Name']            # remove entry with key 'Name'
print(temp_dict)
temp_data = temp_dict.pop("Age")            # returns entry and removes from dictionary
print(temp_dict)
temp_data
del temp_data
temp_dict.clear()                # remove all entries in dict
print(temp_dict)
del temp_dict                    # removes dict from local scope (doesn’t actually delete)<jupyter_output><empty_output><jupyter_text>Iteration Over DictsCan be iterated similar to a list, however prior to version 3.6 it would not keep the order - unless it was an [OrderedDict](https://docs.python.org/2/library/collections.htmlcollections.OrderedDict) datatype.Try this!```pythonphonebook = {'Sally': 8675309, 'Jane': 9745199}for name, number in phonebook.items():    print('You can call {} at {}'.format(name, number))```<jupyter_code>phonebook = {'Sally': 8675309, 'Jane': 9745199}
for name, number in phonebook.items():
    print('You can call {} at {}'.format(name, number))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1280696300901461, 'fraction_numerical': 0.016319552377991917, 'mean_word_length': 4.974930362116992, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 9, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '534828', 'license': 'MIT', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1930, 'n_words': 661, 'path': 'CiscoDevNetCreate2019/Python_Basics_11_-_Variable_Type_DICTIONARY.ipynb', 'repo_name': 'ProsperousHeart/Training-Events', 'size': '6434'}