dict

Dictionaries are mutable unordered collections (they do not record element position or order of insertion) of key-value pairs. Keys within the dictionary must be unique and must be hashable. That includes types like numbers, strings and tuples. Lists and dicts can not be used as keys since they are mutable. Dictionaries in other languages are also called hash tables or associative arrays.

Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as 1 and 1.0) then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.)

Constructors

dict()
Returns a dictionary object.
{} dict comprehension
Returns a dictionary based on existing iterables.
literal syntax
Initializes a new instance of the dict type.

Methods

Contents Access

get
Returns the value for key in the dictionary; if not found returns a default value.
items
Returns a copy of the dictionary’s list of (key, value) pairs.
keys
Returns a copy of the dictionary’s list of keys.
values
Returns a copy of the dictionary’s list of values.

Adding Elements

update
Adds key:value pairs to the dictionary.

Deleting

clear
Removes all items from the dictionary.
pop
Removes the key in the dictionary and returns its value.
popitem
Removes and returns an arbitrary key:value pair from the dictionary.

Information

has_key
Returns a Boolean stating whether the specified key is in the dictionary.

Other

copy
Returns a shallow copy of the dictionary.
fromkeys
Returns a new dictionary with keys from a supplied iterable and values all set to specified value.

Iterators

iteritems
Returns an iterator over the dictionary’s key:value pairs.
itervalues
Returns an iterator over the dictionary’s values.
iterkeys
Returns an iterator over the dictionary’s keys.

Dictionary Views

viewitems
Returns a new view of the dictionary’s items (key:value pairs).
viewvalues
Returns a new view of the dictionary’s values.
viewkeys
Returns a new view of the dictionary’s keys.

Dictionary Views Operators

& (intersection)
Returns only the elements that appear both in the dictview and the specified iterable.
^ (symmetric difference)
Returns the elements that appear in either the dictview or the specified iterable, but not in both.
- (difference)
Returns the elements that appear in the dictview and not in the specified iterable.
| (union)
Returns all the elements that appear in the dictview and the specified iterable.

Functions

len
Returns an int type specifying number of elements in the collection.
min
Returns the smallest item from a collection.
max
Returns the largest item in an iterable or the largest of two or more arguments.
sum
Returns a total of the items contained in the iterable object.
sorted
Returns a sorted list from the iterable.
reversed
Returns a reverse iterator over a sequence.
all
Returns a Boolean value that indicates whether the collection contains only values that evaluate to True.
any
Returns a Boolean value that indicates whether the collection contains any values that evaluate to True.
enumerate
Returns an enumerate object.
zip
Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.

Misc

[] (key lookup)
Returns the value associated with the given key.