Python - Dictionaries Method List

Introduction

Python dictionaries are accessed by key, not offset position.

The following table summarizes some of the most common and representative dictionary operations.

Operation
Interpretation
D = {}
Empty dictionary
D = {'name': 'test', 'age': 40}
Two-item dictionary
E = {'cto': {'name': 'test', 'age': 40}}
Nesting
D = dict(name='test', age=40)
Alternative construction techniques:
D = dict([('name', 'test'), ('age', 40)])
D = dict(zip(keyslist, valueslist))
D = dict.fromkeys(['name', 'age'])
keywords, key/value pairs, zipped key/value pairs, key lists


D['name']
E['cto']['age']
Indexing by key

'age' in D
Membership: key present test
D.keys()
Methods: all keys,
D.values()
all values,
D.items()
all key+value tuples,
D.copy()
copy (top-level),
D.clear()
clear (remove all items),
D.update(D2)
merge by keys,
D.get(key, default?)
fetch by key, if absent default (or None),
D.pop(key, default?)
remove by key, if absent default (or error)
D.setdefault(key, default?)
fetch by key, if absent set default (or None),
D.popitem()
remove/return any (key, value) pair; etc.
len(D)
Length: number of stored entries
D[key] = 42
Adding/changing keys
del D[key]
Deleting entries by key
list(D.keys())
D1.keys() & D2.keys()
Dictionary views (Python 3.X)

D.viewkeys(), D.viewvalues()
Dictionary views (Python 2.7)
D = {x: x*2 for x in range(10)}
Dictionary comprehensions (Python 3.X, 2.7)