Error: " 'dict' object has no attribute 'iteritems' "


I’m trying to use NetworkX to read a Shapefile and use the function write_shp() to generate the Shapefiles that will contain the nodes and edges, but when I try to run the code it gives me the following error:

Traceback (most recent call last):   File
"C:/Users/Felipe/PycharmProjects/untitled/", line 4, in
    nx.write_shp(redVial, "shapefiles")   File "", line
192, in write_shp
    for key, data in e[2].iteritems(): AttributeError: 'dict' object has no attribute 'iteritems'

I’m using Python 3.4 and installed NetworkX via pip install.

Before this error it had already given me another one that said "xrange does not exist" or something like that, so I looked it up and just changed xrange to range in the file, which seemed to solve it.

From what I’ve read it could be related to the Python version (Python2 vs Python3).

Asked By: friveraa



As you are in python3 , use dict.items() instead of dict.iteritems()

iteritems() was removed in python3, so you can’t use this method anymore.

Take a look at Python 3.0 Wiki Built-in Changes section, where it is stated:

Removed dict.iteritems(), dict.iterkeys(), and dict.itervalues().

Instead: use dict.items(), dict.keys(), and dict.values()

Answered By: rafaelc

I had a similar problem (using 3.5) and lost 1/2 a day to it but here is a something that works – I am retired and just learning Python so I can help my grandson (12) with it.

mykey=[key for key,value in mydict2.items()if value==maxval][0]
Answered By: Big Daddy

In Python2, we had .items() and .iteritems() in dictionaries. dict.items() returned list of tuples in dictionary [(k1,v1),(k2,v2),...]. It copied all tuples in dictionary and created new list. If dictionary is very big, there is very big memory impact.

So they created dict.iteritems() in later versions of Python2. This returned iterator object. Whole dictionary was not copied so there is lesser memory consumption. People using Python2 are taught to use dict.iteritems() instead of .items() for efficiency as explained in following code.

import timeit

d = {i:i*2 for i in xrange(10000000)}  
start = timeit.default_timer()
for key,value in d.items():
    tmp = key + value #do something like print
t1 = timeit.default_timer() - start

start = timeit.default_timer()
for key,value in d.iteritems():
    tmp = key + value
t2 = timeit.default_timer() - start


Time with d.items(): 9.04773592949
Time with d.iteritems(): 2.17707300186

In Python3, they wanted to make it more efficient, so moved dictionary.iteritems() to dict.items(), and removed .iteritems() as it was no longer needed.

You have used dict.iteritems() in Python3 so it has failed. Try using dict.items() which has the same functionality as dict.iteritems() of Python2. This is a tiny bit migration issue from Python2 to Python3.

Answered By: Prabhu

In Python2, dictionary.iteritems() is more efficient than dictionary.items() so in Python3, the functionality of dictionary.iteritems() has been migrated to dictionary.items() and iteritems() is removed. So you are getting this error.

Use dict.items() in Python3 which is same as dict.iteritems() of Python2.

Answered By: user6802918

The purpose of .iteritems() was to use less memory space by yielding one result at a time while looping. I am not sure why Python 3 version does not support iteritems()though it’s been proved to be efficient than .items()

If you want to include a code that supports both the PY version 2 and 3,

except NameError:
    iteritems = items

This can help if you deploy your project in some other system and you aren’t sure about the PY version.

Answered By: DivyaMaheswaran

As answered by RafaelC, Python 3 renamed dict.iteritems -> dict.items.
Try a different package version. This will list available packages:

python -m pip install yourOwnPackageHere==

Then rerun with the version you will try after == to install/switch version

Answered By: Punnerud

for me I got the same error but for another simpler reason, this once I try to access python dict in javascript way using .: so I got the error

Error: " 'item' object has no attribute 'data' "

so fix this I had only to use python way item['data']

item = { id: 1, "data": "value"}
print( # Error

item = { id: 1, "data": "value"}
print(item['data']) # value
Answered By: DINA TAKLIT
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.