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.
Typical output in Python 2 looks like:
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.
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.
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:
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 3d.items() - Python 2
d.items()often becomes Python 3list(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 becomeitems().

