<jupyter_start><jupyter_text>Dictioanries - Mapping type*Developed by <NAME>*info: - https://www.w3schools.com/python/python_dictionaries.asp- https://docs.python.org/3/library/stdtypes.html?highlight=stringsmapping-types-dict  DictionaryDictionaries are used to store data values in `key:value,` pairs. Keys are unmutable objects(hashbale) that are mapped with values of any data type (numeric, boolean, lists,strings, dicts, etc). This has similar functionality to the record type in C language the `struct` type.A dictionary is a collection which is ordered*, changeable (Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created) and do not allow duplicates.*As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.Unordered means that the items does not have a defined order, you cannot refer to an item by using an index.*Dictionaries are written with curly brackets, have keys and values, and each value can be retrieved by using the key name:<jupyter_code>car = {
  "brand": "alfa romeo",
  "model": "giulia gta",
  "year": 1965
}
print(car)
print(car["brand"])
print(type(car)) #dictionaries defined as objects with the data type 'dict'
"""Dictionaries are changeable, meaning that we can change, 
add or remove items after the dictionary has been created."""
car = {
  "brand": "alfa romeo",
  "model": "giulia gta",
  "year": 1965
}
#change/update value
car["model"] = "gt junior"
car["year"] = 1972
#add new values
car["color"] = "alfa red"

print(car)
"""Duplicate values will overwrite existing values:"""
car = {
  "brand": "alfa romeo",
  "model": "giulia gta",
  "year": 1965,
  "year": 2018
}
print(car)
"""Dictionary length"""

print(len(car))
"""Any data type"""
car = {
    "brand": "alfa romeo",
    "model": "giulia gta",
    "year": 1965,
    "colors": ["alfa red","montreal green"],
    "petrol": True
}
print(car)<jupyter_output>{'brand': 'alfa romeo', 'model': 'giulia gta', 'year': 1965, 'colors': ['alfa red', 'montreal green'], 'petrol': True}<jupyter_text>Assessing items<jupyter_code>model = car["model"]
#or
model = car.get("model")

print(model)<jupyter_output>giulia gta<jupyter_text>Get keysThe keys() method will return a list of all the keys in the dictionary.For all dict methods you can go to: https://www.w3schools.com/python/python_dictionaries_methods.asp<jupyter_code>keys = car.keys()
print(keys)<jupyter_output>dict_keys(['brand', 'model', 'year', 'colors', 'petrol'])<jupyter_text>Get valuesThe values() method will return a list of all the values in the dictionary.<jupyter_code>values = car.values()
print(values)<jupyter_output>dict_values(['alfa romeo', 'giulia gta', 1965, ['alfa red', 'montreal green'], True])<jupyter_text>Get itemsThe items() method will return each item in a dictionary, as tuples in a list.<jupyter_code>items = car.items()
print(items)
items = list(items) #convert to python list
print("\n",items) 
print("\n>>",items[2]) #now we can use it has a list<jupyter_output>dict_items([('brand', 'alfa romeo'), ('model', 'giulia gta'), ('year', 1965), ('colors', ['alfa red', 'montreal green']), ('petrol', True)])

 [('brand', 'alfa romeo'), ('model', 'giulia gta'), ('year', 1965), ('colors', ['alfa red', 'montreal green']), ('petrol', True)]

>> ('year', 1965)<jupyter_text>Check if key existsTo determine if a specified key is present in a dictionary use the in keyword:<jupyter_code>if "model" in car:
    print("Yes, 'model' exists and is: ", car["model"])<jupyter_output>Yes, 'model' exists and is:  giulia gta<jupyter_text>Remove itemsThere are several methods to remove items from a dictionary.One is the the pop() method removes the item with the specified key name:<jupyter_code>print(car)
car.pop("brand")
print("\n", car)<jupyter_output>{'brand': 'alfa romeo', 'model': 'giulia gta', 'year': 1965, 'colors': ['alfa red', 'montreal green'], 'petrol': True}

 {'model': 'giulia gta', 'year': 1965, 'colors': ['alfa red', 'montreal green'], 'petrol': True}<jupyter_text>Loop DictionaryYou can loop through a dictionary by using a for loop.When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.<jupyter_code>print(car,"\n>>")
#make a pretty print
for key in car.keys(): #or you can use car.values()
    value = car[key]
    print(key, ":" ,value)

# you can also use items
for item in car.items(): 
    key, value = item #this is how you retrieve two values
    print(key, ":" ,value)<jupyter_output>model : giulia gta
year : 1965
colors : ['alfa red', 'montreal green']
petrol : True<jupyter_text>Copy DictionariesYou cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2.There are ways to make a copy, one way is to use the built-in Dictionary method copy().<jupyter_code>car = {
    "brand": "alfa romeo",
    "model": "giulia gta",
    "year": 1965,
}
print(car)
car1 = car #without copying
car2 = car.copy()
car["model"] = "zagato"
print(f"\n C: {car}\n C1: {car1}\n C2: {car2}")<jupyter_output>{'brand': 'alfa romeo', 'model': 'giulia gta', 'year': 1965}

 C: {'brand': 'alfa romeo', 'model': 'zagato', 'year': 1965}
 C1: {'brand': 'alfa romeo', 'model': 'zagato', 'year': 1965}
 C2: {'brand': 'alfa romeo', 'model': 'giulia gta', 'year': 1965}<jupyter_text>Nested DictionariesA dictionary can contain other dictionaries:<jupyter_code>cars = {
    "car": car,
    "car1": car1,
    "car2": car2
}
display(cars)<jupyter_output><empty_output><jupyter_text>Save dictionary JSON fileYou can save directly save the dictionary to a JSON file to have a pretty log:<jupyter_code>import json
with open('cars.json', 'w') as f:
    json.dump(cars, f)<jupyter_output><empty_output><jupyter_text>Exercise  Get the key of a minimum value from the following dictionary using a for loopsample_dict = {  'Physics': 82,  'Math': 65,  'history': 75} For more exercises go to https://www.w3resource.com/python-exercises/dictionary/, https://pynative.com/python-dictionary-exercise-with-solutions/  Answers to the exercises Answer to Exercise<jupyter_code>sample_dict = {
  'Physics': 82,
  'Math': 65,
  'history': 75
}
key="<PASSWORD>"
mini = sample_dict["Physics"] #declare the first minimum; you can also use numpy.NINF()
for i in sample_dict.items():
    k, v = i
    if v< mini:
        mini = v
        key = k
print(key)<jupyter_output>Math
{'fraction_non_alphanumeric': 0.14126506024096386, 'fraction_numerical': 0.017319277108433735, 'mean_word_length': 5.312737642585551, 'pattern_counts': {'":': 23, '<': 42, '<?xml version=': 0, '>': 47, 'https://': 5, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '551359', 'license': 'MIT', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2169, 'n_words': 775, 'path': 'intro_to_programming_1st_year/c11_dictionaries_edited.ipynb', 'repo_name': 'nmc-costa/iclasses', 'size': '6651'}