Debian Codegraph @

docs/other-functionality.rst

raw
1 Other Functionality
2 ===================
3
4 :func:`bidict.inverted`
5 -----------------------
6
7 Bidict provides the :class:`~bidict.inverted` iterator
8 to help you get inverse items from various types of objects.
9
10 Pass in a mapping to get the inverse mapping:
11
12 .. doctest::
13
14 >>> from bidict import inverted
15 >>> it = inverted({1: 'one'})
16 >>> {k: v for (k, v) in it}
17 {'one': 1}
18
19 ...an iterable of pairs to get the pairs' inverses:
20
21 .. doctest::
22
23 >>> list(inverted([(1, 'one'), (2, 'two')]))
24 [('one', 1), ('two', 2)]
25 >>> list(inverted((i*i, i) for i in range(2, 5)))
26 [(2, 4), (3, 9), (4, 16)]
27
28 ...or any object implementing an ``__inverted__`` method,
29 which objects that already know their own inverses (such as bidicts)
30 can implement themselves:
31
32 .. doctest::
33
34 >>> dict(inverted(bidict({1: 'one'})))
35 {'one': 1}
36 >>> list(inverted(OrderedBidict({2: 4, 3: 9})))
37 [(4, 2), (9, 3)]