Python dictionaries

Dictionaries are created using `{}` like this or using `dict()`.

Example:

``````d = {'a':5, 'b':9, 'c':-1}
``````

or you can convert them from tuples:

Example:

``````t = ('a',5), ('b',9), ('c',-1), ('d',4)
print(type(t))
d2 = dict(t)
d2
``````

Output:

``````<class 'tuple'>
{'a': 5, 'b': 9, 'c': -1, 'd': 4}
``````

Get the len of dictionary

Example:

``````d = {'a':5, 'b':9, 'c':-1}
len(d)
``````

Output:

``````3
``````

Get the value from the dictionary

Example:

``````d = {'a':5, 'b':9, 'c':-1}
d['a']
``````

Output:

``````5
``````

What is dictionary comprehension?

In dict comprehension we traverse all the elements and create a new dic `nd`.

Example:

``````d = {'a':5, 'b':9, 'c':-1}
nd = {k:v*v for k,v in d.items()}
nd
``````

Output:

``````{'a': 25, 'b': 81, 'c': 1}
``````

How to invert a dictionary (keys and values)?

As we know how to use dictionary comprehension we can use the same for key-values inversion.

Example:

``````d = {'a':5, 'b':9, 'c':-1}
d_inv = {v: k for k, v in d.items()}
d_inv
``````

Output:

``````{5: 'a', 9: 'b', -1: 'c'}
``````

How to remove elements with specific keys, values?

There is a way to remove the key from the dictionary if we use `del`, `pop`, `popitem` and `clear`:

Example:

``````d = {'a':5, 'b':9, 'c':-1, 'd':4}
del d['d']
print(d)
d.pop('c')
print(d)
d.popitem()
print(d)
d.clear()
print(d)
``````

Output:

``````{'a': 5, 'b': 9, 'c': -1}
{'a': 5, 'b': 9}
{'a': 5}
{}
``````

The other way is to use dictionary comprehension like before with the if condition, but then we deal with the new dictionary:

Example:

``````d = {'a':5, 'b':9, 'c':-1}
nd = {k:v*v for k,v in d.items() if k!='c'}
nd
``````

Output:

``````{'a': 25, 'b': 81}
``````

The same output would be for:

Example:

``````d = {'a':5, 'b':9, 'c':-1}
nd ={k:v*v for k,v in d.items() if v>0}
nd
``````

Get dictionary keys and values

This example dissolves dict to keys and values and creates new exactly the same dictionary from those keys and values:

Example:

``````d ={'a':5, 'b':9, 'c':-1}
k = d.keys()
v = d.values()
k,v
``````

Output:

``````(dict_keys(['a', 'b', 'c']), dict_values([5, 9, -1]))
``````

About the same output you can get with:

Example:

``````d ={'a':5, 'b':9, 'c':-1}
k,v = zip(*d.items())
k,v
``````

Output:

``````(('a', 'b', 'c'), (5, 9, -1))
``````

Convert two sequences (lists) into dictionary

Example:

``````k,v = ['a', 'b', 'c'], [5, 9, -1]
d = dict(zip(k, v))
d
``````

Output:

``````{'a': 5, 'b': 9, 'c': -1}
``````

Sorting a dictionary

While it is not possible to sort a dictionary, since dictionaries are orderless, it is possible to use list comprehension to create new dictionary sorted the way we like. Plain simple `sorted` can be used for that.

Example:

``````d ={'a':5, 'b':9, 'c':-1}
nd = {k:v for k, v in sorted(d.items(), key=lambda _: _[1])}
print(nd)
``````

Output:

``````{'c': -1, 'a': 5, 'b': 9}
``````

The trick is, the way we inserted items to the dictionary is how they will be presented with `print`.

tags: dictionary - dict & category: python