I have a nested dictionary as below
entry = {
0: {"Q": 0},
1: {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0},
2: {
"N": {
"Q": {"E"}
}
},
}
When I try to access only the keys for the key 1, I get the following:
>>> print(entry[1].keys())
dict_keys(['W', 'E', 'N', 'S', 'Q'])
But for key 2 it only returns the top key and not the nested key.
>>> print(entry[2].keys())
dict_keys(['N'])
Why is it not returning the nested key of the dictionary?
dict.keys()method doesn't do that.keysreturns the keys of the dictkeysis called on, not all keys contained at an arbitrary point in the dict. while "Q" is a key which happens to be in the dict, from the perspective ofentry[2]it's part of thevaluesentry.keys()to return?entry[2]['N']['Q']will give you'E'