# 'dict' object has no attribute 'has_key'

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?

`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
``````
• I think `key not in d.keys()` is probably much slower, too, since `key not in d` should be O(1) lookup and I believe `keys` produces a list, which is O(n) lookup (not to mention taking extra space in memory). I could be wrong about that though -- it might still be hashed lookup Nov 16, 2015 at 2:55
• @AdamSmith not in Python 3, `d.keys()` is a view that implements most of the set interface. Jan 24, 2017 at 16:24
• It removed... but why ? Since it make python 2 port to python 3 more work to do.
– 林果皞
Aug 8, 2018 at 15:59
• @林果皞: The whole point of a new major version is that the developers can introduce improvements which may include breaking changes instead of having to support old features as the language matures. This is always a risk which must be considered before upgrading to a new major version. In this case, `in` is shorter and more Pythonic, as well as being consistent with other collections in the language. Aug 8, 2018 at 16:26

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'))
``````
• I think this is the right and easier way to do, Thank you for the answer Jan 19 at 17:29

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
``````

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
``````
• I'm not sure, according to The Zen of Python(PEP 20): "Explicit is better than implicit". I think that if you use the `in` keyword, your intention might not be clear enough what does `if start not in graph:` means? may be `graph` is a list and it checks if there is no such string in the list? On the other hand, if you use syntax like `has_key` (now deprecated) or at least `in graph.keys()` it is more clear that `graph` is a `dict` May 21, 2020 at 20:09

Try:

``````if start not in graph:
``````

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')

``````['A', 'B', 'C', 'D']
{"ft_lang_label":"__label__en","ft_lang_prob":0.7571523,"math_prob":0.6155626,"size":566,"snap":"2022-05-2022-21","text_gpt3_token_len":155,"char_repetition_ratio":0.12455516,"word_repetition_ratio":0.0,"special_character_ratio":0.26678446,"punctuation_ratio":0.18699187,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98176837,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-25T20:53:32Z\",\"WARC-Record-ID\":\"<urn:uuid:cc489578-2cc6-415f-a0f0-ed114451bb6e>\",\"Content-Length\":\"281563\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b66a38e3-f5bd-46ec-a574-67394bacbd6a>\",\"WARC-Concurrent-To\":\"<urn:uuid:0d3df3da-a55c-4446-983d-75622eed8175>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/33727149/dict-object-has-no-attribute-has-key/33727186\",\"WARC-Payload-Digest\":\"sha1:CYRCFER2FDGPVIWTFAJ565JXHGC5LY2H\",\"WARC-Block-Digest\":\"sha1:W2SW3DI5JJ5JOBU6QE5XGMHH74GZZIU7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662593428.63_warc_CC-MAIN-20220525182604-20220525212604-00440.warc.gz\"}"}