How to check if a key exists in an inner dictionary inside a dictionary in python?

前端 未结 3 891
情深已故
情深已故 2021-01-23 14:41

There is a python dictionary:

a = {b:{c:{\"x\":1, \"y\":2, \"z\":3}}}

I want to know if a[b][c][\"z\"] exists, but yet I don\'t kn

3条回答
  •  走了就别回头了
    2021-01-23 14:46

    IF you can't use an exception for some reason (eg. lambda func, list comprehension, generator expression etc)

    value = a.get(b, {}).get(c, {}).get("z", None)
    

    But normally you should prefer to use the exception handler

提交回复
热议问题