'dict' object has no attribute 'has_key'

PythonPython 3.xDictionary

Python Problem Overview


While traversing a graph in Python, a I'm receiving this error:

>'dict' object has no attribute 'has_key'

Here is my code:

def find_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not graph.has_key(start):
        return None
    for node in graph[start]:
        if node not in path:
            newpath = find_path(graph, node, end, path)
            if newpath: return newpath
    return None

The code aims to find the paths from one node to others. Code source: http://cs.mwsu.edu/~terry/courses/4883/lectures/graphs.html

Why am I getting this error and how can I fix it?

Python Solutions


Solution 1 - Python

has_key was removed in Python 3. From the documentation:

> - Removed dict.has_key() – use the in operator instead.

Here's an example:

if start not in graph:
    return None

Solution 2 - Python

In python3, has_key(key) is replaced by __contains__(key)

Tested in python3.7:

a = {'a':1, 'b':2, 'c':3}
print(a.__contains__('a'))

Solution 3 - Python

has_key has been deprecated in Python 3.0. Alternatively you can use 'in'

graph={'A':['B','C'],
   'B':['C','D']}

print('A' in graph)
>> True

print('E' in graph)
>> False

Solution 4 - Python

I think it is considered "more pythonic" to just use in when determining if a key already exists, as in

if start not in graph:
    return None

Solution 5 - Python

Try:

if start not in graph:

For more info see ProgrammerSought

Solution 6 - Python

The whole code in the document will be:

graph = {'A': ['B', 'C'],
             'B': ['C', 'D'],
             'C': ['D'],
             'D': ['C'],
             'E': ['F'],
             'F': ['C']}
def find_path(graph, start, end, path=[]):
        path = path + [start]
        if start == end:
            return path
        if start not in graph:
            return None
        for node in graph[start]:
            if node not in path:
                newpath = find_path(graph, node, end, path)
                if newpath: return newpath
        return None

After writing it, save the document and press F 5

After that, the code you will run in the Python IDLE shell will be:

find_path(graph, 'A','D')

The answer you should receive in IDLE is

['A', 'B', 'C', 'D'] 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshiView Question on Stackoverflow
Solution 1 - PythonjohnnyRoseView Answer on Stackoverflow
Solution 2 - PythonqloveshmilyView Answer on Stackoverflow
Solution 3 - PythonAbhishek PansotraView Answer on Stackoverflow
Solution 4 - PythonKevin SView Answer on Stackoverflow
Solution 5 - PythonGodfreyView Answer on Stackoverflow
Solution 6 - PythonOana RoxanaView Answer on Stackoverflow