Python 2
dict.items()
dict.iteritems()
Python dictionaries
dictionary methods

What is the difference between dict.items and dict.iteritems in Python 2?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python 2, dict.items() and dict.iteritems() both let you work with dictionary key-value pairs, but they return different kinds of objects. The practical difference is memory usage and iteration style: items() builds a full list immediately, while iteritems() yields pairs lazily as you loop.

items() Materializes a List

Calling items() in Python 2 creates a real list of tuple pairs.

python
1d = {"a": 1, "b": 2, "c": 3}
2pairs = d.items()
3
4print(type(pairs))
5print(pairs)

Typical output in Python 2 looks like:

text
<type 'list'>
[('a', 1), ('b', 2), ('c', 3)]

That behavior is convenient when you need list operations such as indexing, slicing, or sorting. The cost is that Python allocates a full list containing every key-value tuple up front.

iteritems() Streams Results

iteritems() returns an iterator object that produces one pair at a time.

python
1d = {"a": 1, "b": 2, "c": 3}
2it = d.iteritems()
3
4print(type(it))
5for key, value in it:
6    print(key, value)

This is usually preferable for a simple one-pass loop because it avoids building the intermediate list first. On large dictionaries, that can save significant memory.

Why the Difference Matters

For a tiny dictionary, either method is fine. For a large dictionary, items() duplicates a lot of information in memory just to support iteration.

python
1totals = {"alice": 10, "bob": 20, "carol": 30}
2
3running_total = 0
4for _, value in totals.iteritems():
5    running_total += value
6
7print(running_total)

This is a good use of iteritems() because the code only needs each pair once. There is no benefit to materializing a list first.

Snapshot Versus Live Iteration

Another difference appears when the dictionary changes during traversal. Because items() creates a separate list, you are iterating over a snapshot. Because iteritems() walks the dictionary lazily, mutating the dictionary during iteration can raise an error or create confusing behavior.

Example with snapshot semantics:

python
1d = {"a": 1, "b": 2, "c": 3}
2
3for key, value in d.items():
4    if value % 2 == 1:
5        del d[key]
6
7print(d)

This works because the loop is traversing a prebuilt list, not the dictionary itself.

That does not mean items() is always safer. It just means its eager behavior changes how mutation interacts with iteration.

Python 3 Changed the Model

This topic matters today mostly because of migration work. Python 3 removed iteritems() entirely, and dict.items() now returns a lightweight view rather than the old Python 2 list.

So the common migration rule is:

  • Python 2 d.iteritems() becomes Python 3 d.items()
  • Python 2 d.items() often becomes Python 3 list(d.items()) if a real list is required

That difference catches people because the method name stayed the same but its behavior changed.

Common Pitfalls

The biggest mistake in Python 2 code is using items() inside large loops when you only need one pass. That creates unnecessary memory pressure for no gain.

Another issue is assuming iteritems() behaves like a list. It does not support indexing or slicing because it is an iterator, not a materialized collection.

Mutation during iteration is another trap. If you need snapshot behavior while removing keys, iterate over d.items() or list(d.iteritems()) instead of changing the dictionary under a live iterator.

Finally, migration can go wrong when old code is copied into Python 3 unchanged. iteritems() does not exist there, and Python 3 items() is not the same object type as Python 2 items().

Summary

  • In Python 2, dict.items() returns a full list of (key, value) tuples.
  • 'dict.iteritems() returns a lazy iterator over those pairs.'
  • Use iteritems() for normal one-pass loops on large dictionaries.
  • Use items() when you truly need snapshot semantics or list behavior.
  • During Python 3 migration, most iteritems() calls should become items().

Course illustration
Course illustration

All Rights Reserved.