Python
AttributeError
dict
iteritems
Python 3

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.

Browse interview questions

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:

python
1data = {"a": 1, "b": 2}
2
3for key, value in data.iteritems():
4    print(key, value)

The fix is usually just:

python
1data = {"a": 1, "b": 2}
2
3for key, value in data.items():
4    print(key, value)

Use items() in Python 3

For most migrations, replace iteritems() with items().

python
1config = {"host": "localhost", "port": 5432}
2
3for key, value in config.items():
4    print(key, value)

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:

python
1data = {"x": 10, "y": 20}
2
3for key in data.keys():
4    print(key)
5
6for value in data.values():
7    print(value)

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:

python
1data = {"a": 1, "b": 2}
2
3for key, value in data.items():
4    if value == 1:
5        del data[key]

Safer:

python
1data = {"a": 1, "b": 2}
2
3for key, value in list(data.items()):
4    if value == 1:
5        del data[key]

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.

python
1def iter_items(d):
2    return getattr(d, "iteritems", d.items)()
3
4data = {"a": 1, "b": 2}
5for key, value in iter_items(data):
6    print(key, value)

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:

  1. replace obvious iteritems() calls with items()
  2. rerun tests
  3. check for mutation-during-iteration cases
  4. 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() and itervalues().
  • Treat this error as a Python 2 to Python 3 migration issue, not a dictionary bug.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.