# dicts.py >>> a = dict(A=1, Z=-1) >>> b = {'A': 1, 'Z': -1} >>> c = dict(zip(['A', 'Z'], [1, -1])) >>> d = dict([('A', 1), ('Z', -1)]) >>> e = dict({'Z': -1, 'A': 1}) >>> a == b == c == d == e # are they all the same? True # They are indeed # zip >>> list(zip(['h', 'e', 'l', 'l', 'o'], [1, 2, 3, 4, 5])) [('h', 1), ('e', 2), ('l', 3), ('l', 4), ('o', 5)] >>> list(zip('hello', range(1, 6))) # equivalent, more pythonic [('h', 1), ('e', 2), ('l', 3), ('l', 4), ('o', 5)] # basic >>> d = {} >>> d['a'] = 1 # let's set a couple of (key, value) pairs >>> d['b'] = 2 >>> len(d) # how many pairs? 2 >>> d['a'] # what is the value of 'a'? 1 >>> d # how does `d` look now? {'a': 1, 'b': 2} >>> del d['a'] # let's remove `a` >>> d {'b': 2} >>> d['c'] = 3 # let's add 'c': 3 >>> 'c' in d # membership is checked against the keys True >>> 3 in d # not the values False >>> 'e' in d False >>> d.clear() # let's clean everything from this dictionary >>> d {} # views >>> d = dict(zip('hello', range(5))) >>> d {'h': 0, 'e': 1, 'l': 3, 'o': 4} >>> d.keys() dict_keys(['h', 'e', 'l', 'o']) >>> d.values() dict_values([0, 1, 3, 4]) >>> d.items() dict_items([('h', 0), ('e', 1), ('l', 3), ('o', 4)]) >>> 3 in d.values() True >>> ('o', 4) in d.items() True # other methods >>> d {'h': 0, 'e': 1, 'l': 3, 'o': 4} >>> d.popitem() # removes a random item (useful in algorithms) ('o', 4) >>> d {'h': 0, 'e': 1, 'l': 3} >>> d.pop('l') # remove item with key `l` 3 >>> d.pop('not-a-key') # remove a key not in dictionary: KeyError Traceback (most recent call last): File "", line 1, in KeyError: 'not-a-key' >>> d.pop('not-a-key', 'default-value') # with a default value? 'default-value' # we get the default value >>> d.update({'another': 'value'}) # we can update dict this way >>> d.update(a=13) # or this way (like a function call) >>> d {'h': 0, 'e': 1, 'another': 'value', 'a': 13} >>> d.get('a') # same as d['a'] but if key is missing no KeyError 13 >>> d.get('a', 177) # default value used if key is missing 13 >>> d.get('b', 177) # like in this case 177 >>> d.get('b') # key is not there, so None is returned # setdefault >>> d = {} >>> d.setdefault('a', 1) # 'a' is missing, we get default value 1 >>> d {'a': 1} # also, the key/value pair ('a', 1) has now been added >>> d.setdefault('a', 5) # let's try to override the value 1 >>> d {'a': 1} # no override, as expected # setdefault example >>> d = {} >>> d.setdefault('a', {}).setdefault('b', []).append(1) >>> d {'a': {'b': [1]}} # union >>> d = {'a': 'A', 'b': 'B'} >>> e = {'b': 8, 'c': 'C'} >>> d | e {'a': 'A', 'b': 8, 'c': 'C'} >>> e | d {'b': 'B', 'c': 'C', 'a': 'A'} >>> {**d, **e} {'a': 'A', 'b': 8, 'c': 'C'} >>> {**e, **d} {'b': 'B', 'c': 'C', 'a': 'A'} >>> d |= e >>> d {'a': 'A', 'b': 8, 'c': 'C'}