In Python Are Dictionaries Much Slower in Performance Compared to Lists or Tuples?

Problem scenario
You want to know how dictionaries perform as iterables in Python. In Python for printing the values of every key-value pair in a dictionary, is it faster or slower than printing every item in a list? How does it compare to a tuple?

Solution
For this example we use integer keys in the dictionary. Keys can be strings or other objects. But for this example, we use whole numbers.

We found that one program that tests all three could not scientific. The order in which the tests are run skew the results. We want a control group. Therefore we have a program generate a file to be used as input for each of the three programs. This way we are using the same input to be somewhat scientific. Ideally the programs would run on a Linux server that is not doing anything else but allowing you to log in and run Python programs. We have three other programs test the input as a dictionary, list and tuple respectively.

Here is the file generation program (file_generator.py):

import random
import string
import sys
import datetime
import time

CONSTANT = 990
def randomword(length):
   letters = string.ascii_lowercase
   return ''.join(random.choice(letters) for i in range(length))

def dictcreator(counter, webstera):
  webstera[counter] = randomword(2)
  if (counter < CONSTANT):
    counter = counter + 1
    return dictcreator(counter, webstera )
  else:
    print("processing...")
    return webstera

def checker(counter, iterable_item, match_counter):
    if (counter < CONSTANT):
        counter = counter + 1
        if iterable_item[counter] == 'az':
            match_counter = match_counter + 1
        return checker(counter, iterable_item, match_counter)
    else:
        print(type(iterable_item))
        return match_counter

adict = dictcreator(0, {})  # This generates a dictionary.

file = open("contint3.txt","w")
for item in adict:
    file.write(str(item))
    file.write(' ')
    file.write(str(adict[item]))
    file.write('\n')
file.close()

Here is the dictionary_tester.py program:

import random
import string
import sys
import datetime
import time
import re

CONSTANT = 990
def checker(counter, iterable_item, match_counter):
    if (counter < CONSTANT):
        counter = counter + 1
        if iterable_item[counter] == 'az':
            match_counter = match_counter + 1
        return checker(counter, iterable_item, match_counter)
    else:
        print(type(iterable_item))
        return match_counter

adict = {}
aa = open("contint3.txt") #.read().replace('{', '')

for line in aa:
    (key, val) = line.split()
    adict[int(key)] = val

listoftimes = []
for x in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
    t1 = datetime.datetime.now()
    dict_result = checker(0, adict, 0)  # adict is a dictionary
    t2 = datetime.datetime.now()
    dict_duration = t2 - t1
    listoftimes.append(dict_duration)
    print("The search operation with the dictionary took ", dict_duration)
    t3 = t2 - t2

total_duration = t3
for item in listoftimes:
    total_duration = item + total_duration

avg_duration = total_duration/10

print("The average duration of 10 runs with a dictionary was ", avg_duration)

Here is the tuple_tester.py program:

import random
import string
import sys
import datetime
import time
import re

CONSTANT = 990
def checker(counter, iterable_item, match_counter):
    if (counter < CONSTANT):
        counter = counter + 1
        if counter < len(iterable_item):
            if iterable_item[counter] == 'az':
                match_counter = match_counter + 1
        return checker(counter, iterable_item, match_counter)
    else:
        print(type(iterable_item))
        return match_counter

atuple = ()
aa = open("contint3.txt")
for line in aa:
    (key, val) = line.split()

listoftimes = []
for x in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
    t1 = datetime.datetime.now()
    tuple_result= checker(0, atuple, 0)  # atuple is a tuple
    t2 = datetime.datetime.now()
    tuple_duration = t2 - t1
    listoftimes.append(tuple_duration)
    print("The search operation with the tuple took ", tuple_duration)
    t3 = t2 - t2

total_duration = t3
for item in listoftimes:
    total_duration = item + total_duration

avg_duration = total_duration/10

print("The average duration of 10 runs with a tuple was ", avg_duration)

Here is the list_tester.py program:

import random
import string
import sys
import datetime
import time
import re

CONSTANT = 990
def checker(counter, iterable_item, match_counter):
    if (counter < CONSTANT):
        counter = counter + 1
        if counter < len(iterable_item):
            if iterable_item[counter] == 'az':
                match_counter = match_counter + 1
        return checker(counter, iterable_item, match_counter)
    else:
        print(type(iterable_item))
        return match_counter

alist = []
aa = open("contint3.txt")

for line in aa:
    (key, val) = line.split()

listoftimes = []
for x in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
    t1 = datetime.datetime.now()
    list_result = checker(0, alist, 0)  # alist is a list
    t2 = datetime.datetime.now()
    list_duration = t2 - t1
    listoftimes.append(list_duration)
    print("The search operation with the list took ", list_duration)
    t3 = t2 - t2

total_duration = t3
for item in listoftimes:
    total_duration = item + total_duration

avg_duration = total_duration/10

print("The average duration of 10 runs with a list was ", avg_duration)

When doing the tests, we found the list [built-in] iterable to be slightly faster than the tuple [built-in] iterable. Dictionary [built-in] iterables performed slightly worse. If you want to study Python closely, you can view a list of books here.

Leave a comment

Your email address will not be published. Required fields are marked *