How to get nested dictionary key value with .get()

前端 未结 4 1525
臣服心动
臣服心动 2021-02-07 02:48

With a simple dictionary like:

myDict{\'key1\':1, \'key2\':2}

I can safely use:

print myDict.get(\'key3\')

an

4条回答
  •  时光说笑
    2021-02-07 03:32

    Use exceptions:

    try:
        print myDict['key1']['attr3']
    except KeyError:
        print "Can't find my keys"
    

提交回复
热议问题