top of page

Python Programming Help

Public·2 members

'dict' object has no attribute 'has_key'

has_key() is method defined in python 2 so not available in Python 3.


so you can use directly check key in dictionary like this


Suppose graph is dictionary type

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

start = 'A'

if start  in graph:
    print(start,' is found in graph')
else:
    print(start,' is not found in graph')
    
    

Here is code example


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
find_path(graph, 'A', 'D')

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



Ask your question in comment box below our expert will answer


1061 Views
bottom of page