What is the difference between dict.items and dict.iteritems in Python 2?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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().
Related reading
- What is the difference between Dijkstra and Prim's algorithm?
- What is the difference between HashSetT and ListT?
- What is the difference between Linear search and Binary search?
- What is the difference between List of T and Collectionof T?
- What is the difference between flatten and ravel functions in numpy?
- What is the difference between is None and None
- What is the difference between List.of and Arrays.asList?
- What is the difference between message queue and message broker?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.