I have the following piece of code. article_as_dict
is a dictionary that I'm receiving from an external source:
final_dict["short_title"] = article_as_dict["short_title"]
try:
final_dict["picture_url"] = article_as_dict["main_image"]["img"][-1]["link"]
except IndexError:
final_dict["picture_url"] = None
I discovered recently that I also need to account for a possible KeyError
, is the block below the most pythonic way to do this?
final_dict["short_title"] = article_as_dict["short_title"]
try:
final_dict["picture_url"] = article_as_dict["main_image"]["img"][-1]["link"]
except IndexError:
final_dict["picture_url"] = None
except KeyError:
final_dict["picture_url"] = None
I don't want a naked except
clause because it's bad practice.