<jupyter_start><jupyter_text>5.1 Dictionaries, Maps, and Hashtables<jupyter_code>phonebook = {
'bob': 7387,
'alice': 3719,
'jack': 7052,
}

squares = {x: x * x for x in range(6)}

phonebook['alice']<jupyter_output><empty_output><jupyter_text>Python’s dictionaries are indexed by keys that can be of any hashable type 2 : **A hashable object has a hash value which never changes during its lifetime, and it can be compared to other objects**.In addition, hashable objects which compare as equalmust have the same hash value.<jupyter_code>import collections


# collections.OrderedDict – Remember the
# Insertion Order of Keys

d = collections.OrderedDict(one=1, two=2, three=3)

d.keys()
# collections.defaultdict – Return Default Values
# for Missing Keys
from collections import defaultdict
dd = defaultdict(list)

dd['dogs'].append('Rufus')
dd['dogs'].append('Kathrin')
dd['dogs'].append('Mr Sniffles')

print(dd['dogs'])

dd['cats'].append('Princess')
dd['cats'].append('Witch')
dd['cats'].append('Bilbo')

print(dd['cats'])
# collections.ChainMap – Search Multiple
# Dictionaries as a Single Mapping
from collections import ChainMap

dict1 = {'one': 1, 'two': 2}
dict2 = {'three': 3, 'four': 4}
chain = ChainMap(dict1, dict2)

ChainMap({'one': 1, 'two': 2}, {'three': 3, 'four': 4})
# ChainMap searches each collection in the chain
# from left to right until it finds the key (or fails):
chain['three']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14206128133704735, 'fraction_numerical': 0.02158774373259053, 'mean_word_length': 6.149253731343284, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '294188', 'license': 'MIT', 'n_tokens_mistral': 511, 'n_tokens_neox': 472, 'n_words': 156, 'path': '2022/Python-Tricks-Book/Chapter5-Data-Structures/dicts-maps-hashtables.ipynb', 'repo_name': 'Muramatsu2602/python-study', 'size': '1436'}