<jupyter_start><jupyter_text>[Dictionaries](https://docs.python.org/3/library/stdtypes.htmldict) Collections of `key`-`value` pairs.   Here's a video tutorial on `Dictionaries` in Python. It uses this notebook so you can code along with the video.<jupyter_code>## Run this cell (shift+enter) to see the video

from IPython.display import IFrame
IFrame("https://www.youtube.com/embed/fsjcmwOlCNI", width="814", height="509")<jupyter_output><empty_output><jupyter_text>![image.png](attachment:image.png)  Dictionaries vs. ListsDictionaries and lists share the following characteristics:- Both are mutable.- Both are dynamic. They can grow and shrink as needed.- Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.Dictionaries differ from lists primarily in how elements are accessed:- List elements are accessed by their position in the list, via indexing.- Dictionary elements are accessed via keys. ![image.png](attachment:image.png)  Why use a dictionary? ![image.png](attachment:image.png)  Creating a dictionaryDictionaries can be created by placing a comma-separated list of key: value pairs within braces, for example: {'jack': 4098, 'sjoerd': 4127} or {4098: 'jack', 4127: 'sjoerd'}, or by the dict constructor.<jupyter_code>my_dict1 = {'jack': 4098, 'sjoerd': 4127}
my_dict2 = dict({4098: 'jack', 4127: 'sjoerd'})

print(my_dict1)
print(my_dict2)
my_empty_dict = {}  # alternative: my_empty_dict = dict()
print('dict: {}, type: {}'.format(my_empty_dict, type(my_empty_dict)))
my_empty_dict[1] = 'one'
print(my_empty_dict)<jupyter_output><empty_output><jupyter_text>Initialization<jupyter_code>dict1 = {'value1': 1.6, 'value2': 10, 'name': '<NAME>'}
dict2 = dict(value1=1.6, value2=10, name='<NAME>')

print(dict1)
print(dict2)

print('equal: {}'.format(dict1 == dict2))
print('length: {}'.format(len(dict1)))<jupyter_output><empty_output><jupyter_text>Creating dictionary from lists<jupyter_code>keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]

sampleDict = dict(zip(keys, values))
print(sampleDict)
help(zip)<jupyter_output><empty_output><jupyter_text>`dict.keys(), dict.values(), dict.items()`<jupyter_code>print('keys: {}'.format(dict1.keys()))  # list of all keys
print('values: {}'.format(dict1.values())) # list of all values
print('items: {}'.format(dict1.items())) # list of all key,value pairs<jupyter_output><empty_output><jupyter_text>Restrictions on Dictionary KeysAlmost any type of value can be used as a dictionary key in Python. You just saw this example, where integer, float, and Boolean objects are used as keys:<jupyter_code># integers, floats, or even boolean values can be used as keys

test_d1 = {42: 'aaa', 2.78: 'bbb', True: 'ccc'}
print(test_d1)
# You can even use built-in objects like types and functions

test_d2 = {int: 1, float: 2, bool: 3}
print(test_d2)<jupyter_output><empty_output><jupyter_text>Rules for dictionary keys - - First, a given key can appear in a dictionary only once. Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn’t make sense to map a particular key more than once.- A dictionary key must be of a type that is immutable. You have already seen examples where several of the immutable types you are familiar with—integer, float, string, and Boolean—have served as dictionary keys. A tuple can also be a dictionary key, because tuples are immutable.By contrast, there are no restrictions on dictionary values. Literally none at all. A dictionary value can be any type of object Python supports, including mutable types like lists and dictionaries, and user-defined objects<jupyter_code>#bad_dict = {['my_list'], 'value'}  # Raises TypeError<jupyter_output><empty_output><jupyter_text>Values can be mutable<jupyter_code>good_dict = {'my key': ['Python', 'is', 'still', 'cool']}
print(good_dict)<jupyter_output><empty_output><jupyter_text>Accessing and setting values<jupyter_code>my_dict = {}
my_dict['key1'] = 'value1'
my_dict['key2'] = 99
my_dict['key1'] = 'new value'  # overriding existing value
print(my_dict)
print('value of key1: {}'.format(my_dict['key1']))
print(my_dict['key2'])<jupyter_output><empty_output><jupyter_text>Accessing a nonexistent key will raise `KeyError` (see [`dict.get()`](dict_get) for workaround):<jupyter_code>print(my_dict['nope'])<jupyter_output><empty_output><jupyter_text>Deleting<jupyter_code>my_dict = {'key1': 'value1', 'key2': 99, 'keyX': 'valueX'}
del my_dict['keyX']
print(my_dict)

# Usually better to make sure that the key exists (see also pop() and popitem())
key_to_delete = 'my_key'
if key_to_delete in my_dict:
    del my_dict[key_to_delete]
else:
    print('{key} is not in {dictionary}'.format(key=key_to_delete, dictionary=my_dict))<jupyter_output><empty_output><jupyter_text>Dictionaries are mutable<jupyter_code>my_dict = {'ham': 'good', 'carrot': 'semi good'}
my_other_dict = my_dict
my_other_dict['carrot'] = 'super tasty'
my_other_dict['sausage'] = 'best ever'
print('my_dict: {}\nother: {}'.format(my_dict, my_other_dict))
print('equal: {}'.format(my_dict == my_other_dict))
print(id(my_other_dict))
print(id(my_dict))<jupyter_output><empty_output><jupyter_text>Create a new `dict` if you want to have a copy:<jupyter_code>my_dict = {'ham': 'good', 'carrot': 'semi good'}
my_other_dict = dict(my_dict)
my_other_dict['beer'] = 'decent'
print('my_dict: {}\nother: {}'.format(my_dict, my_other_dict))
print('equal: {}'.format(my_dict == my_other_dict))<jupyter_output><empty_output><jupyter_text>`dict.get()`Returns `None` if `key` is not in `dict`. However, you can also specify `default` return value which will be returned if `key` is not present in the `dict`.<jupyter_code>my_dict = {'a': 1, 'b': 2, 'c': 3}
d = my_dict.get('d')
#d1 = my_dict['d']
print('d: {}'.format(d))

d = my_dict.get('d', 'key does not exist, value not found!')
print('d: {}'.format(d))<jupyter_output><empty_output><jupyter_text>`dict.pop()`<jupyter_code>my_dict = dict(food='ham', drink='beer', sport='football')
print('dict before pops: {}'.format(my_dict))

food = my_dict.pop('food')
print('food: {}'.format(food))
print('dict after popping food: {}'.format(my_dict))

food_again = my_dict.pop('food', 'default value for food')
print('food again: {}'.format(food_again))
print('dict after popping food again: {}'.format(my_dict))<jupyter_output><empty_output><jupyter_text>`dict.setdefault()`Returns the `value` of `key` defined as first parameter. If the `key` is not present in the dict, adds `key` with default value (second parameter).<jupyter_code>my_dict = {'a': 1, 'b': 2, 'c': 3}
a = my_dict.setdefault('a', 'my default value')
d = my_dict.setdefault('d', 'my default value')
print('a: {}\nd: {}\nmy_dict: {}'.format(a, d, my_dict))<jupyter_output><empty_output><jupyter_text>`dict.update()`Merge two `dict`s<jupyter_code>dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3}
dict1.update(dict2)
print(dict1)

# If they have same keys:
dict1.update({'c': 4})
print(dict1)<jupyter_output><empty_output><jupyter_text>Another way to merge two dictionaries<jupyter_code>dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}


dict3 = {**dict1, **dict2}
print(dict3)
print(dict1)
print(dict2)<jupyter_output><empty_output><jupyter_text>Looping through `dict`s<jupyter_code>my_dict = {'hacker': True, 'age': 72, 'name': '<NAME>'}

# Looping through all values in my_dict

for key in my_dict:
    print(key, my_dict[key])
# Looping through all key,value pairs in my_dict

for key, val in my_dict.items():
    print('{}={}'.format(key, val))<jupyter_output><empty_output><jupyter_text>Run the following code to test yourself on Dictionaries in Python<jupyter_code>!rm -r Basic_Python_Data
!git clone https://github.com/anikannal/Basic_Python_Data
import Basic_Python_Data.quiz
Basic_Python_Data.quiz.quiz_me('QB_Dictionary.xlsx')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup