<jupyter_start><jupyter_text>Define simple printing functions<jupyter_code>from __future__ import print_function

import json 

def print_dict(dd): 
    print(json.dumps(dd, indent=2))<jupyter_output><empty_output><jupyter_text>Constructing and allocating dictionaries The syntax for dictionaries is that `{}` indicates an empty dictionary<jupyter_code>d1 = dict() 
d2 = {}

print_dict(d1)
print_dict(d2)<jupyter_output>{}
{}<jupyter_text>There are multiple ways to construct a dictionary when the key/value pairs are known beforehand. The following two snippets are equivalent.<jupyter_code>d3 = {
    'one': 1, 
    'two': 2
}

print_dict(d3)
d4 = dict(one=1, two=2)

print_dict(d4)<jupyter_output>{
  "one": 1,
  "two": 2
}<jupyter_text>Often an ordered list of keys and values are available as lists, and it is desirable to create a dictionary from these lists. There are a number of ways to do this, including:<jupyter_code>keys = ['one', 'two', 'three']
values = [1, 2, 3]

d5 = {key: value for key, value in zip(keys, values)}
print_dict(d5)<jupyter_output>{
  "one": 1,
  "two": 2,
  "three": 3
}<jupyter_text>Adding new data to the dict<jupyter_code>d1['key_1'] = 1
d1['key_2'] = False

print_dict(d1)<jupyter_output>{
  "key_1": 1,
  "key_2": false
}<jupyter_text>Dictionaries are a dynamic data type, and any object can be used as a value type, including `integers`, `float`s, `list`s, and other `dict`s, for example:<jupyter_code>d1['list_key'] = [1, 2, 3]
print_dict(d1)
d1['dict_key'] = {'one': 1, 'two': 2}
print_dict(d1)
del d1['key_1']
print_dict(d1)<jupyter_output>{
  "key_2": false,
  "list_key": [
    1,
    2,
    3
  ],
  "dict_key": {
    "one": 1,
    "two": 2
  }
}<jupyter_text>Accessing the dataIt is always possible to get access to the key/value pairs that are contained in the dictionary, and the following functions help with this:<jupyter_code>print(d1.keys())
for item in d1:
    print(item)
d1['dict_key']['one']<jupyter_output><empty_output><jupyter_text>Iterating over key/values The following two cells are nearly equivalent. In order to understand how they differ, it will be helpful to confer with Python documentation on iterators and generatorshttp://anandology.com/python-practice-book/iterators.html<jupyter_code>for key, value in d1.items(): 
    print(key, value)
for key, value in d1.iteritems():  # Only in Python2 (.items() returns an iterator in Python3)
    print(key, value)
print(d1.keys())
print(d1.values())<jupyter_output>dict_keys(['key_2', 'list_key', 'dict_key'])
dict_values([False, [1, 2, 3], {'one': 1, 'two': 2}])<jupyter_text>Filtering and mapping dictionaries<jupyter_code>def dict_only(key_value): 
    return type(key_value[1]) is dict

print('All dictionary elements:')
print(list(filter(dict_only, d1.items())))
print('Same as above, but with inline function (lambda):')
print(filter(lambda key_value: type(key_value[1]) is dict, d1.items()))<jupyter_output>Same as above, but with inline function (lambda):
<filter object at 0x10d57ac50>
{'fraction_non_alphanumeric': 0.13331117021276595, 'fraction_numerical': 0.02526595744680851, 'mean_word_length': 5.731543624161074, 'pattern_counts': {'":': 12, '<': 31, '<?xml version=': 0, '>': 31, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '437808', 'license': 'MIT', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1081, 'n_words': 316, 'path': '01_data_ingress/02_dicts.ipynb', 'repo_name': 'njtwomey/ADS', 'size': '3008'}