Cheat Sheets

A collection of cheat sheets.

Hosted on GitHub Pages — Theme by mattgraham

Dictionaries

Official Documentation

Creation

# Literal
d = { key1 => value1, key2 => value2 }

# From list of pairs
d = dict([ (key1, value1), (key2, value2) ])

Operations

Syntax Semantics
xs[k] Read value associated with k
xs[k] = v Add/overwrite value associated with k
len(d) Number of key/value pairs in d
d.keys() List of keys
d.values() List of values
d.items() Sequence of key/value pairs
del(d[k]) Removes key/value pair whose key is k
d.clear() Empty dictionary
d.copy() Make a copy
k in d Checks if k is a key in d