Dictionaries
Python's efficient key/value hash table structure is called a dict. The contents of a dict can be written as a series of key: value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}.
Looking up or setting a value in a dict uses square brackets, e.g. dict['foo'] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use in to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case).
type=codeblock|id=py3_dict_init_and_lookup|autocreate=python3## Can build up a dict by starting with the the empty dict {}## and storing key/value pairs into the dict like this:## dict[key] = value-for-that-keydict = {}dict['a'] = 'alpha'dict['g'] = 'gamma'dict['o'] = 'omega'print(dict) ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}print(dict['a']) ## Simple lookup, returns 'alpha'dict['a'] = 6 ## Put new key/value into dictprint('a' in dict) ## True## print(dict['z']) ## Throws KeyErrorif 'z' in dict: print(dict['z']) ## Avoid KeyErrorprint(dict.get('z')) ## None (instead of KeyError)