Replace:

if key in output.keys():
    output[key][pop] = qtl[2]
else:
    output[key] = {pop : qtl[2]}

by:

try:
    output[key][pop] = qtl[2]
except KeyError:
    output[key] = {pop : qtl[2]}



EDIT:

Actually, this is even nicer (and faster!):

if key in output:
    output[key][pop] = qtl[2]
else:
    output[key] = {pop : qtl[2]}

The same script:

  • With the first solution (key in output.keys()): time real: 6m33.843s
  • With the second solution (try/except): time real: 0m1.017s
  • With the third solution (key in output): time real: 0m0.949s

Thanks to mcepl for telling me the try/except wasn't a really nice solution.