Search code examples
pythonpython-3.xattributeerror

python 'dict' object has no attribute 'viewkeys'


i am newbie in python and i was running program and maybe it is written in python2.X which is provided by my friend,

but it was not running because i was getting error of dict object has no attribute viewkeys originally when i have ran the script error was as below:

Traceback (most recent call last):
  File "test.py", line 236, in <module>
    for key in (Data_Pe.keys() | Data.viewkeys()):
AttributeError: 'dict' object has no attribute 'viewkeys'

in it Data_pe and Data both are dict object.


Solution

  • I have tried to find on stackoverflow but i have not found any question related to this so i have asked and answered it myself, i have found the answer on the python documentation which was as below

    dict

    Fixes dictionary iteration methods. dict.iteritems() is converted to dict.items(), dict.iterkeys() to dict.keys(), and dict.itervalues() to dict.values(). Similarly, dict.viewitems(), dict.viewkeys() and dict.viewvalues() are converted respectively to dict.items(), dict.keys() and dict.values(). It also wraps existing usages of dict.items(), dict.keys(), and dict.values() in a call to list.

    so in short in python3.X version it is changed from :

    dict.viewkeys() to dict.keys()