June 2018
# dictionaries are hash tables, key:value pairs
dct = {
"Adam": ["[email protected]", 2445055],
"Bard": "[email protected]"
}
print(dct)
# {'Adam': ['[email protected]', 2445055], 'Bard': '[email protected]'}
print(dct["Adam"])
# ['[email protected]', 2445055]
print(dct["Adam"][1])
# 2445055
# update value
dct["Bard"] = "[email protected]"
print(dct)
# {'Adam': ['[email protected]', 2445055], 'Bard': '[email protected]'}
# add key:value pair
dct["Cole"] = "[email protected]"
print(dct)
# {'Adam': ['[email protected]', 2445055], 'Bard': '[email protected]', 'Cole': '[email protected]'}
# remove key:value pair
del dct["Cole"]
print("Cole" in dct)
# False
print("Adam" in dct)
# True
# create dictionary from a list of tuples
dct_list_tuples = dict([(1, "x"), (2, "y"), (3, "z")])
print(dct_list_tuples)
# {1: 'x', 2: 'y', 3: 'z'}