<jupyter_start><jupyter_text>Beginning Programming in Python Advanced Dictionaries CSE20 - Spring 2021Interactive Slides: [https://tinyurl.com/cse20-spr21-adv-dictionaries](https://tinyurl.com/cse20-spr21-adv-dictionaries)  Dictionaries- An unordered collection of key/value pairs- Keys can be any hashable type- Values can be any type  Dictionary Creation<jupyter_code>a_dict = {
    "one": 1,
    "two": 2,
}

print(a_dict)<jupyter_output><empty_output><jupyter_text>Dictionary Creation<jupyter_code>a_dict = dict(
    one=1,
    two=2
)

print(a_dict)<jupyter_output><empty_output><jupyter_text>Dictionary Creation<jupyter_code>a_dict = dict([
    ("one", 1),
    ("two", 2)
])

print(a_dict)<jupyter_output><empty_output><jupyter_text>Editing a Dictionary<jupyter_code>a_dict = dict(one=1, two=2)
a_dict["three"] = 3
print(a_dict)

del a_dict["one"]

print(a_dict)<jupyter_output><empty_output><jupyter_text>Dictionary Methods- `dict.clear()` removes all key/value pairs from `dict`- `dict.get(key, default)` get the value associated with `key` if it exists, otherwise return `default`- `dict.update(other_dict)` overwrites key/values in `dict` with keys/values from `other_dict` and adds them if they don't exist- `dict.pop(key, default)` gets value associated with `key` and removes `key`, if it doesn't exists return `default` - See more methods in the [Python Documentation](https://docs.python.org/3/library/stdtypes.htmltypesmapping)  Iterating Through Dictionaries- Use dictionary views to iterate through dictionaries- `dict.keys()` returns a dictionary view of the keys in `dict`- `dict.values()` returns a dictionary view of the values in `dict`- `dict.items()` returns a dictionary view of the (key, value) pairs in `dict`   Iterating Through Dictionaries<jupyter_code>a_dict = dict(a="Apple", b="Banana", c="Carrot")

for key in a_dict.keys():
    print(key, end=" ")
print()
for value in a_dict.values():
    print(value, end=" ")
print()
for key, value in a_dict.items():
    print((key, value), end=" ")<jupyter_output><empty_output><jupyter_text>Dictionary Comprehensions- Dictionary comprehensions are like list comprehensions in form, but use curly braces and set key value pairs```pythondict_var = {key:value for key, value in key_value_pairs}dict_var = {key:value for key, value in key_value_pairs if boolean_expression}```<jupyter_code>kvs = [("one", 1, True), ("two", 2, False), ("three", 3, True), ("four", 4, False)]
dict_var = {key_value[0]:key_value[1:] for key_value in kvs}
even_dict_var = {key:value for key, value, valid in kvs if valid}

print(dict_var)
print(even_dict_var)
dict_var.update(even_dict_var)
print(dict_var)<jupyter_output>{'one': (1, True), 'two': (2, False), 'three': (3, True), 'four': (4, False)}
{'one': 1, 'three': 3}
{'one': 1, 'two': (2, False), 'three': 3, 'four': (4, False)}<jupyter_text>`zip` Function- The `zip()` function iterates over multiple collections simultaneously- If one of the collections is shorter than the others `zip` will go until the shortest one is exhausted<jupyter_code>fruits = ["Apple", "Banana", "Orange", "Kiwi"]
prices = [1.00, 2.25, 1.50]

for fruit, price in zip(fruits, prices):
    print(fruit, price)<jupyter_output>Apple 1.0
Banana 2.25
Orange 1.5<jupyter_text>`zip` Function- The zip() function iterates over multiple collections simultaneously- If one of the collections is shorter than the others `zip` will go until the shortest one is exhausted<jupyter_code>fruits = ["Apple", "Banana", "Orange"]
prices = [1.00, 2.25, 1.50]

fruit_prices = {fruit:price for fruit, price in zip(fruits, prices)}
print(fruit_prices)<jupyter_output>{'Apple': 1.0, 'Banana': 2.25, 'Orange': 1.5}
{'fraction_non_alphanumeric': 0.1404846174789001, 'fraction_numerical': 0.019602504764497687, 'mean_word_length': 6.038314176245211, 'pattern_counts': {'":': 2, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, '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': '101796', 'license': 'MIT', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1246, 'n_words': 377, 'path': 'CSE20_Advanced_Dictionaries.ipynb', 'repo_name': 'ryanhausen/cse-20-spring-2021', 'size': '3673'}