Python - Dictionary
class dict(object):
- dict() -> new empty dictionary.
- dict(**kwargs) -> new dictionary initialized with the
name=value
pairs in the keyword argument list. e.g.dict(a=1, b=2)
.- dict(mapping, **kwargs) -> new dictionary initialized from a
mapping object’s(key, value)
pairs. e.g.{'a': 1}
,dict({'a': 1})
.- dict(iterable, **kwargs) -> new dictionary iterable object.
Each item in the iterable must itself be an iterable with exactly two objects.
e.g.dict([('a', 1)])
,dict([('a', 1), ('b', 2)])
.
If the key is duplicated, the value from the keyword argument replaces
the value from the positional argument.
e.g.dict([('a', 0), ('b', 2)], a=1, c=3)
<=>{'a': 1, 'b': 2, 'c': 3}
len(d)
del d[key]
iter(d)
Return an iterator over the keys of the dictionary.
This is the shortcut foriter(d.keys())
(Python3)/iterkeys()
(Python2).
Create
d.copy()
Return a shallow copy of the dictionary.
d.fromkeys(seq[, value])
Create a new dictionary with keys from
seq
and all of values set tovalue
.fromkeys()
is a class method that return a new dictionary.value
defaults toNone
.
Delete
d.clear()
Remove all items from the dictionary.
d.pop(key[, default])
If the
key
in the dictionary, remove it and return its value, else returndefault
.
Ifdefault
is not given andkey
is not in the dictionary, aKeyError
is raised.d.popitem()
Remove and return a arbitrary
(key, value)
pair from dictionary.
If the dictionary is empty, callingpopitem()
raises aKeyError
.
Update
d.update([other])
Update the dictionary with the key/value pairs from
other
,
overwriting the existing keys. ReturnNone
.update()
accepts either another dictionary object or an iterable of
key/value pairs (as tuple or other iterables of length two).
If the keyword arguments are specified, the dictionary is then updated
with those key/value pairs:d.update(red=1, blue=2)
.d.setdefault(key[, default])
If the
key
in the dictionary, return its value. If not, insertkey
with
a value ofdefault
and returndefault
,default
defaults toNone
.
Retrieve
d.get(key[, default])
Return the value for
key
ifkey
is in the dictionary, elsedefault
.
Ifdefault
is not given, it defaults toNone
, so that this method never raises aKeyError
.
Python3
d.keys()
Return a new view of the dictionary’s keys.
d.values()
Return a new view of the dictionary’s values.
d.items()
Return a new view of the dictionary’s items (
(key, value)
pairs).
Dictionaries compare equal if and only if they have the same (key, value)
pairs.
Order comparisons(‘<’, ‘<=’, ‘>’, ‘>=’) raise TypeError
.
Python2
d.keys()
Return a copy of dictionary’s list of keys.
d.values()
Return a copy of dictionary’s list of values.
d.items()
Return a copy of dictionary’s list of
(key, value)
pairs.d.viewkeys()
Return a new view of the dictionary’s keys.
d.viewvalues()
Return a new view of the dictionary’s values.
d.viewitems()
Return a new view of the dictionary’s items (
(key, value)
pairs).d.iterkeys()
Return a iterator over the dictionary’s keys.
d.itervalues()
Return a iterator over the dictionary’s values.
d.iteritems()
Return a iterator over the dictionary’s
(key, value)
pairs.d.has_key(key)
Test for the presence of
key
in the dictionary.has_key()
is deprecated in favor ofkey in d
.
Dictionary view objects
The objects returned by dict.keys()
, dict.values()
and dict.items()
(Python2: dict.viewkeys()
, dict.viewvalues()
and dict.viewitems()
) are view object.
They provide a dynamic view on the dictionary’s entries, which means that
when dictionary change, the view reflects these changes.1
2
3
4
5
6# Python3
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
keys = d.keys()
print(keys) # ['a', 'b', 'c', 'd']
del d['d']
print(keys) # ['a', 'b', 'c']
len(dictview)
Return the number of entries in the dictionary.
iter(dictview)
Return an iterator over the keys, values or items
(represented as tuples of(key, value)
) in the dictionary.
Iterating views while adding or deleting entries in the dictionary may
raise aRuntimeError
or fail to iterate over all entries.
x in dictview
Return
True
ifx
is in the underlying dictionary’s keys, values or
items (in the latter case,x
should be a(key, value)
tuple).
Keys and items views are set-like since their entries are unique and hashable.
Values views are not treated as set-like since the entries are generally not unique.
For set-like views, all of the operations defined for the set are available(e.g. &
, |
, -
, ^
).