<jupyter_start><jupyter_text>Mapping Keys to Multiple Values in a Dictionary  Problem: Make a dictionary that maps keys to more than one value Solution:> A dictionary is a mapping where each key is mapped to a single value.To map keys to multiple values, we need to store the values in another list. - Example:<jupyter_code>d = {
    'a' : [1, 2, 3],
    'b' : [4, 5]
}

e = {
    'a' : {1, 2, 3},
    'b' : {4, 5}
}<jupyter_output><empty_output><jupyter_text>__NOTE__- Use a list when we want the insertion order of the items. - Use a set if we want to eliminate duplicates. Construct dictionaries- Use the `defaultdict` in the `collections` module. A feature of the `defaultdict` is that it automatically initializes the first value, so the focus is on adding items. - For example:<jupyter_code>from collections import defaultdict

#as a list
d = defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['b'].append(4)

#as a set
d1 = defaultdict(set)
d1['a'].add(1)
d1['a'].add(2)
d1['b'].add(4)<jupyter_output><empty_output><jupyter_text>`defaultdict` will automatically create dictionary entries for keys that can be accessed later. - This can be avoided by using `setdefault()`.- Example:<jupyter_code>dnew ={} # a regular dictionary

d.setdefault('a', []).append(1)

d.setdefault('a', []).append(2)

d.setdefault('b', []).append(4)<jupyter_output><empty_output><jupyter_text>- Using the `defualtdict` leads to having cleander code!<jupyter_code>d = defaultdict(list)
for key, value in pairs:
    d[key].append(value)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13161290322580646, 'fraction_numerical': 0.014838709677419355, 'mean_word_length': 5.08235294117647, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '110262', 'license': 'MIT', 'n_tokens_mistral': 544, 'n_tokens_neox': 522, 'n_words': 181, 'path': 'data structures & algorithms/6.ipynb', 'repo_name': 'Gth1205/py-cookbook', 'size': '1550'}