title: Why does Python handle a KeyError differently than an IndexError (in this case)?
body: <p>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.</p>
  
  <p><strong>Dictionary KeyError Handling</strong></p>
  
  <pre><code>existing_dict = {"spam": 1, "eggs": 2}
  existing_dict["foo"] = existing_dict["foo"] if not KeyError else 3
  </code></pre>
  
  <p>Returns <code>{"spam": 1, "eggs": 2, "foo": 3}</code></p>
  
  <p>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.</p>
  
  <p><strong>List IndexError Handling</strong> (also true for tuples)</p>
  
  <pre><code>existing_list = ["spam","eggs"]
  existing_list[2] = existing_list[2] if not IndexError else ["foo"]
  </code></pre>
  
  <p>Returns <code>IndexError: list assignment index out of range</code></p>
  
  <p>It's not difficult at all to get around this specific error (<a href="https://stackoverflow.com/a/24512699/2628402">answer here</a>), 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.</p>