title: Python - key error when using "if in dict"
body: <p>I am receiving the following error when running a script to parse contents of an XML file. </p>
  
  <pre><code>if iteration.findtext("Iteration_query-def") in ecdict:
  KeyError: 'XLOC_000434'
  </code></pre>
  
  <p>I was under the impression that using "if in dict" would mean that if the key is not found in the dictionary, the script will continue past the if statement and proceed with the rest of the code. Below is the problematic section of the code I am using. I realise this is quite a basic question, but I am unsure what else I can say, and I don't understand why I am receiving this error. </p>
  
  <pre><code>import xml.etree.ElementTree as ET
  tree = ET.parse('507.FINAL_14.2.14_2_nr.out_fmt5.out')
  blast_iteration = tree.find("BlastOutput_iterations")
  for iteration in blast_iteration.findall("Iteration"):
          query = iteration.findtext("Iteration_query-def").strip().strip("\n")
          if query in score:
                  continue
          if iteration.findtext("Iteration_message") == "No hits found":
                  if iteration.findtext("Iteration_query-def") in tair:
                          tairid = tair[iteration.findtext("Iteration_query-def")][0]
                          tairdes = tair[iteration.findtext("Iteration_query-def")][1]
                  else:
                                          tairid = "-"
                                          tairdes = "-"
                  goterms = ""
                  ecterms = ""
                  if iteration.findtext("Iteration_query-def") in godict:
                                          for x in godict[iteration.findtext("Iteration_query-def")][:-1]:
                                                  goterms = goterms + x + ";"
                                          goterms = goterms + godict[iteration.findtext("Iteration_query-def")][-1]
                  else:
                                          goterms = "-"
                  if iteration.findtext("Iteration_query-def") in ecdict:
                                          for x in ecdict[iteration.findtext("Iteration_query-def")][:-1]:
                                                  ecterms = ecterms + x + ";"
                                          ecterms = ecterms + ecdict[iteration.findtext("Iteration_query-def")][-1]
                  else:
                                          ecterms = "-"
                  if iteration.findtext("Iteration_query-def") in godescr:
                          desc = godescr[iteration.findtext("Iteration_query-def")]
                  else:
                          desc = "-"
                  n += 1
                  p = "PvOAK_up"+str(n) + "\t" + tranlen[iteration.findtext("Iteration_query-def")] + "\t" + orflen[iteration.findtext("Iteration_query-def")] + "\t" + "-" + "\t" + "-" + "\t" + tairid  + "\t" + tairdes + "\t" + goterms + "\t" + ecterms + "\t" + desc + "\t" + str(flower[query][2]) + "\t" + str('{0:.2e}'.format(float(flower[query][1]))) + "\t" + str('{0:.2f}'.format(float(flower[query][0]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][2]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][1]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][0])))
                  print p
  </code></pre>
  
  <p>Hope you can help,</p>
  
  <p>Thanks.</p>
  
  <p>edit: I should say that godict and ecdict were previously created as follows - I can submit the entire code if needs be:</p>
  
  <pre><code>godict = {}
  ecdict = {}
  godescr = {}
  f = open("507.FINAL_14.2.14_2_nr.out_fmt5.out.annot")
  for line in f:
          line = line.split("\t")
          if len(line) &gt; 2:
                  godescr[line[0]] = line[2]
          line[1] = line[1].strip("\n")
          if line[1].startswith("EC"):
                  if line[0] in ecdict:
                          a = ecdict[line[0]]
                          a.append(line[1])
                          ecdict[line[0]] = a
                  else:
                          ecdict[line[0]] = [line[1]]
          else:
                  if line[0] in godict:
                          a = godict[line[0]]
                          a.append(line[1])
                          godict[line[0]] = a
                  else:
                          godict[line[0]] = [line[1]]
  </code></pre>
  
  <p>Traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "2d.test.py", line 170, in &lt;module&gt;
      p = "PvOAK_up"+str(n) + "\t" + tranlen[iteration.findtext("Iteration_query-def")] + "\t" + orflen[iteration.findtext("Iteration_query-def")] + "\t" + "-" + "\t" + "-" + "\t" + tairid  + "\t" + tairdes + "\t" + goterms + "\t" + ecterms + "\t" + desc + "\t" + str(flower[query][2]) + "\t" + str('{0:.2e}'.format(float(flower[query][1]))) + "\t" + str('{0:.2f}'.format(float(flower[query][0]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][2]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][1]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][0])))
  KeyError: 'XLOC_000434'
  </code></pre>