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

前端 未结 4 1539
臣服心动
臣服心动 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:11

    That's normal since key3 doesn't exist so

    myDict.get('key3')
    

    returns none..

    NoneType object has no attribute..

    So you have to store the value of myDict.get('key3'), test if it's not null and then use the get method on the stored item

提交回复
热议问题