Florian Florian - 4 months ago 20
Python Question

One-liner to save value of if statement?

Is there a smart way to write the following code in three or four lines?

a=l["artist"]
if a:
b=a["projects"]
if b:
c=b["project"]
if c:
print c


So I thought for something like pseudocode:

a = l["artist"] if True:

Answer

I don't necessarily think that this is better but you could do:

try:
    c = l["artist"]["projects"]["project"]
except (KeyError, TypeError) as e:
    print e
    pass
Comments