Iterating over dictionary or its items

Question:

What’s a better practice and what’s faster:

  • iterating over dictionary (practically by its keys):
    for key in dictionary: ...
  • or iterating over its items:
    for key, value in dictionary.items(): ...?

Using dict.items() seems to be a more clean way to iterate over a dictionary, but isn’t it slower because of creating another object (dict.items) (I suppose it’s negligible, but I had to ask)? Or maybe it’s already done with initializing the dictionary?
Also, in the first way, accessing a value by its key shouldn’t affect the efficiency because the operation is O(1).

Asked By: maciejwww

||

Answers:

Let’s run some tests.

from time import perf_counter

i = {i: 10 for i in range(10**7)}

def t1():
    p1 = perf_counter()
    for key in i:
        i[key]
    print("key in i:",perf_counter() - p1)

def t2():
    p1 = perf_counter()
    for key, value in i.items():
        value
    print("key, value in i.items()", perf_counter() - p1)

t1()
t2()

OUTPUT

key in i: 0.2863648850000118
key, value in i.items() 0.19483049799998753

As you can see, iterating with the ‘items’ method is significantly faster. That’s because when we use key in dict, we iterate through keys while ignoring value; value should be taken directly from the dictionary using dict[x] or dict.get(x) which is by default, do all hash table steps:

  • calls hash function with your attribute x
  • searches into the hash table’s array which is hidden by default and we can’t see it because it’s implementation detail
  • if there are any collisions, they should be solved

However, when we use items, it iterates through the dictionary and collects keys with the values, so we do not need to perform all of the hash table steps for each item.

Answered By: Karen Petrosyan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.