Project: Reverse a dict in Python 2.7 & 3

Fork Download
View on GitHub

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

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

  1. Results
    1. Major updates
    2. Table 1: Avg run times of different methods (Python 2)
    3. Table 2: Avg run times of different methods (Python 3)
  2. Method 1: unique values, solution based on dict comprehension
    1. Python 2.7 with dict.iteritems()
    2. Python 2.7 with dict.items()
    3. Python 3
  3. Method 2: non-unique values
    1. Python 2.7 with dict.get()
    2. Python 3 with dict.get()
    3. Python 2.7 with dict.setdefault()
    4. Python 3 with dict.setdefault()
  4. Method 3: type and order preserved
    1. Python 2.7
    2. Python 3
  5. References
test

Results

Major updates

2018-09-16:
test

Table 1 Average running times (µsec) of different methods
of reversing a dict in Python 2.7
Click on the method name to know more about its implementation
test
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 257412670901
Method 2: dict.get,iteritems 83810410127703
Method 2: setdefault,iteritems 6568539108348
Method 3: map,iteritems 89610789146115

Table 2 Average running times (µsec) of different methods
of reversing a dict in Python 3
Click on the method name to know more about its implementation
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 417494966599
Method 2: setdefault 333417158629
Method 3: map 312336646961

test

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()}
Method 1 of reversing a dict with unique values and using dict.iteritems()
code @ jdoodle.com (Python 2.7.15)
test

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()}
Method 1 of reversing a dict with unique values and using dict.items()
test

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 1 of reversing a dict with unique values
code @ jdoodle.com (Python 3.6.5)
test

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)
Method 2 of reversing a dict with non-unique values
code @ jdoodle.com (Python 2.7.15)
test

Python 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)
Method 2 of reversing a dict with non-unique values
code @ jdoodle.com (Python 3.6.5)
test

Python 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)
Method 2 of reversing a dict with non-unique values using dict.setdefault
code @ jdoodle.com (Python 2.7.15)
test

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)
Method 2 of reversing a dict with non-unique values using dict.setdefault
code @ jdoodle.com (Python 3.6.5)


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)
Method 3 of reversing a dict with unique values using map(reversed, iter)
code @ jdoodle.com (Python 2.7.15)
test

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)
Method 3 of reversing a dict with unique values using map(reversed, iter)
code @ jdoodle.com (Python 3.6.5)


References