# Python: Dictionary Methods

By Xah Lee. Date: . Last updated: .

## Length

`len(d)`
Return number of items in d. For example, `len({"a":8,"b":9})`⇒ 2

## Get, Set, Delete

`d[k]`
Return the value of key k if exist, else KeyError.
```xx = {"a": 1, "b": 2}
print(xx["b"] == 2)```
`d.get(k)`
Return the value of key k if exist, else return `None`.
```xx = {"a": 1, "b": 2}
print(xx.get("b") == 2)```
`d.get(k, v)`
Return the value of key k if exist, else return v.
```xx = {"a": 1, "b": 2}
print(xx.get("c", 8) == 8)```
`d[k] = v`
Set a value v.
`del d[k]`
Remove the value of key k if exist, else KeyError.

## Check Existence

`k in d`
Return `True` if d has a key k, else `False`.
`k not in d`
The opposite of `in`

## Get All Keys, Get All Keys Values

`d.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'])
```
`d.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])
```
`d.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

`d.pop(k)`
Remove and return its value if key k exist, else KeyError.
`d.pop(k, v)`
Remove and return its value if key k exist, else v.
`d.popitem()`
Remove and return arbitrary (key, value) pair. If d is empty, KeyError.

## Set Value, Update

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

Return `None`.

v 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 xdict.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, Copy

`d.clear()`
Remove all items. [see Python: dict={} vs dict.clear()]
`d.copy()`
Return a shallow copy of d. [see How to Copy a Nested List in Python?]
`d.fromkeys(seq)`
Return a new dictionary with keys from sequence seq (list or tuple). The values are all `None`.
`d.fromkeys(seq, v)`
Return a new dictionary with keys from sequence seq. The values are all v.
```# 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

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

## Change Lists into Dictionary

Example of using `zip(…)`. It does transposition.

```# convert 2 lists into a dictionary, use zip()

aa = [1, 2, 3]
bb = ["a", "b", "c"]

print(dict(zip(aa, bb)))
# {1: 'a', 2: 'b', 3: 'c'}
```
```# example of using zip()

aa = [1, 2, 3, 4]
bb = ["a", "b", "c", "d"]
cc = [10, 20, 30, 40]

print(zip(aa, bb, cc))
# <zip object at 0x106f40b88>

print(list(zip(aa, bb, cc)))
# [(1, 'a', 10), (2, 'b', 20), (3, 'c', 30), (4, 'd', 40)]
```

## Python 2, Loop Thru Key/Value Pairs

These are not in python 3.

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