I come across this issue on a regular basis, hopefully someone has an elegant solution...
x = {'a':1, 'b':{'c':2}}
now:
x.get('a', None) = 1
x.get('b', None).get('c', None) = 2
what about:
x.get('a', None).get('c', None) =
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-278862f593e8> in <module>()
----> 1 x.get('a', None).get('c')
AttributeError: 'int' object has no attribute 'get'
#
Is there a simple pythonic one-liner for handling cases like this? (without using a bunch of if statements)
Thanks!
#
Thanks for all the comments/feedback. I'm looking for an elegant one-liner, as I come across this on a regular basis. Obviously this is a toy-example, which was constructed just to explain the problem. I would prefer not to use try/catch blocks or wrap the code in long if statements. Thanks!