Error 'dict' object has no attribute 'iteritems'
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The error 'dict' object has no attribute 'iteritems' almost always means Python 2 code is being run under Python 3. In Python 2, dict.iteritems() returned an iterator over key-value pairs. In Python 3, that method was removed because dict.items() already provides efficient iterable behavior.
Why the Error Happens
In Python 2, these methods were distinct:
- '
items()returned a list' - '
iteritems()returned an iterator'
In Python 3, dictionary view objects replaced that split design, so items() already behaves like an efficient iterable view.
This old code fails in Python 3:
The fix is usually just:
Use items() in Python 3
For most migrations, replace iteritems() with items().
This is the idiomatic Python 3 solution and should be your default.
The Same Migration Pattern Applies to Other Dict Methods
If the codebase is old, you may also see:
- '
iterkeys()->keys()' - '
itervalues()->values()'
Example:
These methods now return dynamic view objects rather than the Python 2 list-based behavior.
Be Careful When Mutating During Iteration
Python 3 dictionary views are live views of the dictionary. If you modify the dictionary while iterating, you can get runtime errors or confusing behavior.
Unsafe:
Safer:
This distinction matters more in Python 3 because items() no longer materializes a separate list by default.
Writing Cross-Version Compatibility Code
If you still support both Python 2 and Python 3 in an old codebase, a compatibility helper can reduce repetition.
That said, for active modern codebases, it is usually better to complete the Python 3 migration rather than keep compatibility helpers indefinitely.
Modern Refactoring Strategy
A practical upgrade sequence is:
- replace obvious
iteritems()calls withitems() - rerun tests
- check for mutation-during-iteration cases
- remove old Python 2 compatibility assumptions
If the codebase is large, automated migration tools can help, but manual review is still important for behavioral differences around list versus view semantics.
Why items() Is Usually Enough
Many developers worry that replacing iteritems() with items() will hurt performance because Python 2 items() used to allocate a list. In Python 3, that concern usually does not apply. items() returns a view and is appropriate for normal iteration.
So in Python 3:
- use
items()for iteration - wrap in
list(...)only when you explicitly need a snapshot
That keeps the code both correct and idiomatic.
Common Pitfalls
The biggest mistake is assuming the error is about dictionaries being malformed. It is almost always a version mismatch between Python 2-era code and Python 3 runtime.
Another issue is replacing iteritems() with items() but forgetting that items() now returns a live view, not a list snapshot.
Developers also sometimes keep compatibility wrappers longer than necessary, which makes modern Python code harder to read.
Summary
- '
iteritems()is a Python 2 method and does not exist on Python 3 dictionaries.' - Replace it with
items()in modern code. - Use
list(d.items())only when you need a fixed snapshot during iteration. - Expect similar migration changes for
iterkeys()anditervalues(). - Treat this error as a Python 2 to Python 3 migration issue, not a dictionary bug.
Related reading
- Error dictionary update sequence element 0 has length 1; 2 is required on Django 1.4
- ERROR Directory is not installable. Neither 'setup.py' nor 'pyproject.toml
- Error Expected 2D array, got 1D array instead Using OneHotEncoder
- Error Failed to create temp directory CUsersuserAppDataLocalTempconda-RANDOM
- Error filename.whl is not a supported wheel on this platform
- Error from tensorflow.examples.tutorials.mnist import input_data
- Error Import Error No module named numpy on Windows
- Error importing BERT module 'tensorflow._api.v2.train' has no attribute 'Optimizer
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.