I was trying out some various one-line solutions to the problem of defining a variable only if it does not already exist and noticed that Python handles dicts and lists/tuples differently. These errors seem entirely parallel to me, so I'm confused why there is discrepancy.
Dictionary KeyError Handling
existing_dict = {"spam": 1, "eggs": 2}
existing_dict["foo"] = existing_dict["foo"] if not KeyError else 3
Returns {"spam": 1, "eggs": 2, "foo": 3}
Notice that I'm referencing a non-existing key in both left and right hand sides; Python has no problem handling the KeyError in either clause where it appears.
List IndexError Handling (also true for tuples)
existing_list = ["spam","eggs"]
existing_list[2] = existing_list[2] if not IndexError else ["foo"]
Returns IndexError: list assignment index out of range
It's not difficult at all to get around this specific error (answer here), but I'm curious why there is a difference in these cases. In both situations, there seems to be an error in both assignee/assignment clauses with one "if not" error catch.