Python: Dictionary Methods

By Xah Lee. Date: . Last updated: .

Length

len(dict)
Return number of key in dict.

Get

dict[key]
Return the value of key key if exist, else KeyError.
xx = {"a": 1, "b": 2}
print(xx["b"])
# 2
dict.get(key)
Return the value of key key if exist, else return None.
xx = {"a": 1, "b": 2}
print(xx.get("b"))
# 2
dict.get(key, default)
Return the value of key key if exist, else return default.
xx = {"a": 1, "b": 2}
print(xx.get("c", 8))
# 8

Set

dict[key] = val
Set a value val.

Delete

del dict[key]
Remove the value of key key if exist, else KeyError.

Check Existence

key in dict
Return True if dict has a key key, else False.
key not in dict
Return True if key not exist.

Get All Keys, Get Values

dict.keys()
Return a iterator that's all key's values. (each element is a new copy)
dd = {"a": 1, "b": 2, "c": 3}
print(dd.keys())
# dict_keys(['a', 'b', 'c'])
dict.values()
Return a iterator that's all key's values. (each element is a new copy)
dd = {"a": 1, "b": 2, "c": 3}

print(dd.values())
# dict_values([1, 2, 3])
dict.items()
Return a iterator of 2-tuples, each is (key, value). (each element is a new copy)
dd = {"a": 1, "b": 2, "c": 3}

print(dd.items())
# dict_items([('a', 1), ('b', 2), ('c', 3)])

Pop, Update

dict.pop(key)
Remove and return its value if key key exist, else KeyError.
dict.pop(key, val)
Remove and return its value if key key exist, else val.
dict.popitem()
Remove and return arbitrary (key, value) pair. If dict is empty, KeyError.

Set Value, Update

dict.setdefault(key)
If key key exist, return its value, else, add key with a value of None.
dict.setdefault(key, val)
If key key exist, return its value, else, insert key with a value of val
dict.update(val)
Update the dictionary with the key/value pairs from val, overwriting existing keys.

Return None.

val can be a dictionary or iterable (list, tuple) where each element is iterable of length 2, or can be a key1=val1, key2=val2, …

# example of dict.update

xx = {"a": 1}

xx.update([[3, 4], ("a", 2)])
print(xx == {"a": 2, 3: 4})

xx.update([(5, 6), (7, 8)])
print(xx == {"a": 2, 3: 4, 5: 6, 7: 8})

xx.update(aa=8, bb=9)
print(xx == {"a": 2, 3: 4, 5: 6, 7: 8, "aa": 8, "bb": 9})

Clear

dict.clear()
Remove all items. 〔see Python: dictionary.clear vs Set Dictionary to Empty

Copy

dict.copy()
Return a shallow copy of dict. 〔see Python: Copy Nested List, Shallow/Deep Copy
dict.fromkeys(seq)
Return a new dictionary with keys from sequence seq (list or tuple). The values are all None.
dict.fromkeys(seq, val)
Return a new dictionary with keys from sequence seq. The values are all val.
# example of xdictionary.fromkeys

xx = {"a": 1, "b": 2, "c": 3}

h2 = xx.fromkeys([8, 9, "a"])
print(h2 == {8: None, 9: None, "a": None})

h3 = xx.fromkeys([8, 9, 10], "x")
print(h3 == {8: "x", 9: "x", 10: "x"})

print(xx == {"a": 1, "b": 2, "c": 3})

Dictionary View

dict.viewitems()
A view to a kill 😨
dict.viewkeys()
A view to a kill 😨
dict.viewvalues()
A view to a kill 😨

Python 2, Loop Thru Key/Value Pairs

These are not in python 3.

dict.has_key(key)
Return true if key key exist in dict.
dict.iterkeys()
Return a iterator. Each element is a key. A short syntax is iter(dict)
dict.itervalues()
Return a iterator. Each element is a key's value.
dict.iteritems()
Return a iterator. Each element is (key, value) pair.

Python, Data Structure