Skip to content
MrS0m30n3 edited this page Oct 9, 2016 · 5 revisions

TwoWayOrderedDict

TwoWayOrderedDict it's a custom dictionary in which you can get the key:value relationship but you can also get the value:key relationship. It also remembers the order in which the items were inserted and supports almost all the features of the build-in dict.


class TwoWayOrderedDict(**kwargs)
class TwoWayOrderedDict(iterable, **kwargs)
class TwoWayOrderedDict(mapping, **kwargs)

Return an instance of a dict subclass, supporting the usual dict methods. A TwoWayOrderedDict is a dict that supports only one-to-one relationships which means that each key can map only to one single value and each value can map only to one single key. It also remembers the order in which the keys were inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.

Note

The TwoWayOrderedDict constructor and update() method both accept keyword arguments, but their order is lost because Python’s function call semantics pass-in keyword arguments using a regular unordered dictionary.

[Unordered Examples]

>>>d = TwoWayOrderedDict(a=1, b=2, c=3)
TwoWayOrderedDict([('a', 1), ('c', 3), ('b', 2)])

>>>d = TwoWayOrderedDict({'a': 1, 'b': 2, 'c': 3})
TwoWayOrderedDict([('a', 1), ('c', 3), ('b', 2)])

[Ordered Examples]

>>>d = TwoWayOrderedDict([('a', 1), ('b', 2), ('c', 3)])
TwoWayOrderedDict([('a', 1), ('b', 2), ('c', 3)])

>>>d = TwoWayOrderedDict(zip(['a', 'b', 'c'], [1, 2, 3]))
TwoWayOrderedDict([('a', 1), ('b', 2), ('c', 3)])

len(d)
Return the number of items in the dictionary d.

d[key | value]
Return the corresponding key-value based on the given input. Raises a KeyError if key-value is not in the map.

>>>d['a']
1

>>>d[1]
'a'

d[key | value] = somevalue
Set d[key | value] to somevalue.

>>>d['a'] = 5
TwoWayOrderedDict([('a', 5), ('b', 2), ('c', 3)])

>>>d[1] = 4
TwoWayOrderedDict([('b', 2), ('c', 3), (1, 4)])

del d[key | value]
Remove d[key | value] from d. Raises a KeyError if key-value is not in the map.

>>> del d['a']
TwoWayOrderedDict([('b', 2), ('c', 3)])

>>> del d[1]
TwoWayOrderedDict([('b', 2), ('c', 3)])

key | value in d
Return True if key-value in d else False.

>>> 'a' in d
True

>>> 1 in d
True

clear()
Remove all items from the dictionary.

copy()
Return a shallow copy of the dictionary.

get(key | value[, default])
Return the corresponding key-value based on the given input. If the given input does not exist in the map return default. If default is not given, it defaults to None, so that this method never raises a KeyError.

>>> d.get('a')
1

>>> d.get(1)
'a'

items()
Return a new view of the dictionary’s items ((key, value) pairs).

>>> d.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])

keys()
Return a new view of the dictionary’s keys.

>>> d.keys()
dict_keys(['a', 'b', 'c'])

values()
Return a new view of the dictionary’s values.

>>> d.values()
dict_values([1, 2, 3])

pop(key | value[, default])
Remove and return the corresponding key-value based on the given input. If the given input does not exist in the map return default. If default is not given and the given input does not exist, a KeyError is raised.

>>> d.pop('a')
1

>>> d.pop(1)
'a'

popitem(last=True)
Remove and return a (key, value) pair. In TwoWayOrderedDict the pairs are returned in LIFO order if last is True or FIFO order if False.

>>> d.popitem()
('c', 3)

>>> d.popitem(last=False)
('a', 1)

setdefault(key | value[, default])
Return the corresponding key-value based on the given input. If the given input does not exist in the map insert a new entry with a value of default and return default. default defaults to None.

>>> d.setdefault('a')
1

>>> d.setdefault(1)
'a'

>>> d.setdefault('d', 4)
TwoWayOrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)])

update([other])
Update the dictionary with the key-value pairs from other, overwriting existing keys. Return None.

update() accepts either another dictionary object or an iterable of key-value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key-value pairs.

>>>d.update(a=10, b=20)
TwoWayOrderedDict([('a', 10), ('b', 20), ('c', 3)])

reversed(d)
Return a generator with all the keys in reversed order.


classmethod fromkeys(seq[, value])
fromkeys() method does not work the same way as the dict fromkeys(), because in the TwoWayOrderedDict each new item in the seq has the same value with the previous one. That means that the previous value will be overwritten because TwoWayOrderedDict supports only one-to-one relationships.

>>> a = TwoWayOrderedDict.fromkeys(['a', 'b', 'c'], 1)
>>> a
# ('c', 1) overwrites ('b', 1) which overwrites ('a', 1)
TwoWayOrderedDict([('c', 1)])

Further notes

  • All the iter*, view* methods (Python 2.*) raise NotImplementedError exception. Use the equivalent items, keys, values methods.

  • In TwoWayOrderedDict, both the keys AND the values must be hashable.

Clone this wiki locally