<jupyter_start><jupyter_text>DictionariesDictionaries are efficient containers (objects) that map keys to values<jupyter_code>identity = {"firstname":"Barack", "lastname":"Obama", "age":55, "married":True}
print(identity)
type(identity)
len(identity)<jupyter_output><empty_output><jupyter_text>1. Accessing<jupyter_code>#You can access to a value by its key
print(identity["firstname"])
print(identity["lastname"])
#You can assign new values
identity["mandates"] = 2

print(identity)
#You can amend or edit an entry similary
identity["age"] = 57

print(identity)
#You can check if a dictionaries contains a key
print("has a dog" in identity)
print("married" in identity)
#Trying to access an inexistent key raises an error
print(identity["address"])
#You can delete an entry using the del keyword
del identity["married"]

print(identity)<jupyter_output>{'firstname': 'Barack', 'lastname': 'Obama', 'age': 57, 'mandates': 2}<jupyter_text>2. MethodsDictionaries, like everything else in Python are objects and have methods<jupyter_code>#Unsure if the key exists? Use the .get method and get a default value if it doesn't exist
print(identity.get("firstname", "whoops"))
print(identity.get("height", "unknown"))
#update (in-place) a dictionary with another
currencies = {"EUR":"Euro","CNY":"Chinese New Year?"}
others = {"GBP":"British Pound", "CNY":"Chinese Yuan"}

currencies.update(others)

#Notice how it overwrote the CNY
print(currencies)
print(others)
#Copy
duplicate = identity.copy()

print(identity)<jupyter_output>{'firstname': 'Barack', 'lastname': 'Obama', 'age': 57, 'mandates': 2}<jupyter_text>3. Iterating over dictionary<jupyter_code>for field in identity: 
    print("The person's {} is {}".format(field, identity[field]))
#Get a list of values
for currency in currencies.values():
    print(currency)
#Get a list of key, value tuples
for ticker, currency in currencies.items():
    print("{} is the ticker for the {}".format(ticker, currency))
    
#Get a list of keys
for field in identity.keys():
    print(field)<jupyter_output>firstname
lastname
age
mandates
{'fraction_non_alphanumeric': 0.1333012512030799, 'fraction_numerical': 0.006737247353224254, 'mean_word_length': 6.904942965779468, 'pattern_counts': {'":': 8, '<': 14, '<?xml version=': 0, '>': 14, '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': '457586', 'license': 'MIT', 'n_tokens_mistral': 691, 'n_tokens_neox': 643, 'n_words': 214, 'path': '13 - Dictionaries.ipynb', 'repo_name': 'dschenck/Python-crash-course', 'size': '2078'}