This project's goal is to compute average run times of different methods of reversing a
dict
's keys and values in Python 2 & 3
-
method 1: makes use of
dict
comprehension, and the dictionary must contain unique values - method 2: the dictionary doesn't contain unique values and saves all the keys with the same values in a list
-
method 3: makes use of
map(reversed, iter)
, useful when the type and order of the original dictionary must be preserved (e.g.OrderedDict
)
I ran some tests, and method 1 is the one that provides the best average run time for reversing the keys and values of a dictionary. See Table 1 (Python 2) and Table 2 (Python 3) for the average run times of the different methods based on the number of items.
Check the project's README for details on how to install and use the program.
You can also check my blog post Python tips: reverse a dictionary where I discuss the implementation of the different methods and the results of the comparaisons of the methods based on their average run times.
Note: I tested the code with Python 2.7.15 and 3.6.5
Contents
- Results
- Method 1: unique values, solution based on
dict
comprehension - Method 2: non-unique values
- Method 3: type and order preserved
- References
Results
Major updates
2018-09-16:- I updated the code base and re-ran the
python commands to re-populate the following two tables with new results. The
changes consisted in implementing the important missing option
--use_setdefault
which I thought was already implemented when I generated the results the first time. Big mistake on my part! Thus the old results for the rows Method 2:setdefault
were generated actually withdict.get()
instead ofdict.setdefault()
:( - Also, I factorized methods.py
where the different
dict
-reversing methods are defined by putting all the common code from Python 2 & 3 methods into base classes.
Py2 Method | Avg time (µsec), 1k items, 100k times | Avg time (µsec), 10k items, 1k times | Avg time (µsec), 100k items, 1k times |
---|---|---|---|
Method 1: dict compre.,iteritems |
218 | 2666 | 45301 |
Method 1: dict compre.,items |
257 | 4126 | 70901 |
Method 2: dict.get ,iteritems |
838 | 10410 | 127703 |
Method 2: setdefault ,iteritems |
656 | 8539 | 108348 |
Method 3: map ,iteritems |
896 | 10789 | 146115 |
Py3 Method | Avg time (µsec), 1k items, 100k times | Avg time (µsec), 10k items, 1k times | Avg time (µsec), 100k items, 1k times |
---|---|---|---|
Method 1: dict compre. | 90 | 905 | 18488 |
Method 2: dict.get |
417 | 4949 | 66599 |
Method 2: setdefault |
333 | 4171 | 58629 |
Method 3: map |
312 | 3366 | 46961 |
Method 1: unique-values, solution based on dict
Python 2.7 with dict.iteritems()
my_dict = { 'a': 1, 'b':2, 'c': 3, 'd':4, 'e':5} inv_dict = {v: k for k, v in my_dict.iteritems()}
dict
with unique values and using dict.iteritems()
code @ jdoodle.com (Python 2.7.15)
Python 2.7 with dict.items()
my_dict = { 'a': 1, 'b':2, 'c': 3, 'd':4, 'e':5} inv_dict = {v: k for k, v in my_dict.items()}
dict
with unique values and using dict.items()
Python 3
my_dict = { 'a': 1, 'b':2, 'c': 3, 'd':4, 'e':5} inv_dict = {v: k for k, v in my_dict.items()}
Method 2: non-unique values
Python 2.7 with dict.get()
my_dict = {1: 'a', 2:'b', 3: 'c', 4: 'a', 5: 'c'} inv_dict = {} for k, v in my_dict.iteritems(): inv_dict[v] = inv_dict.get(v, []) inv_dict[v].append(k)
dict
with non-unique valuesPython 3 with dict.get()
from collections import OrderedDict def reverse_dict(orig_dict): inv_dict = {} for k, v in orig_dict.items(): inv_dict[v] = inv_dict.get(v, []) inv_dict[v].append(k) my_dict = OrderedDict({1: 'a', 2:'b', 3: 'c', 4: 'a', 5: 'c'}) reverse_dict(my_dict)
dict
with non-unique valuesPython 2.7 with dict.setdefault()
my_dict = {1: 'a', 2:'b', 3: 'c', 4: 'a', 5: 'c'} inv_dict = {} for key, value in my_dict.iteritems(): inv_dict.setdefault(value, []).append(key)
dict
with non-unique values using dict.setdefault
Python 3 with dict.setdefault()
my_dict = {1: 'a', 2:'b', 3: 'c', 4: 'a', 5: 'c'} inv_dict = {} for key, value in my_dict.items(): inv_dict.setdefault(value, []).append(key)
dict
with non-unique values using dict.setdefault
Method 3: type and order preserved
Python 2.7
def reverse_mapping(f): return f.__class__(map(reversed, f.iteritems())) my_dict = {1: 'a', 2:'b', 3: 'c', 4: 'd', 5: 'e'} inv_dict = reverse_mapping(my_dict)
dict
with unique values using map(reversed, iter)
code @ jdoodle.com (Python 2.7.15)
Python 3
def reverse_mapping(f): return f.__class__(map(reversed, f.items())) my_dict = {1: 'a', 2:'b', 3: 'c', 4: 'd', 5: 'e'} inv_dict = reverse_mapping(my_dict)
dict
with unique values using map(reversed, iter)
code @ jdoodle.com (Python 3.6.5)