title: How to use dictionaries with a list to synthesize and print inputs from multiple files into a single line for each key in output?
body: <p>Let's say I have a file that looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Gene.name</th>
  <th style="text-align: center;">Experiment.1</th>
  <th style="text-align: right;">Experiment.2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A1BG</td>
  <td style="text-align: center;">0.031474</td>
  <td style="text-align: right;">0.05776</td>
  </tr>
  <tr>
  <td style="text-align: left;">ZNF621</td>
  <td style="text-align: center;">0.091025</td>
  <td style="text-align: right;">0.33516</td>
  </tr>
  <tr>
  <td style="text-align: left;">ARHGAP12</td>
  <td style="text-align: center;">0.97852</td>
  <td style="text-align: right;">0.14098</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>and so on…</p>
  <p>And another file that looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Gene Name</th>
  <th style="text-align: center;">Gene description</th>
  <th style="text-align: right;">Chromosome number</th>
  <th style="text-align: right;">Chromosome location</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A1BG</td>
  <td style="text-align: center;">alpha-1B-glycoprotein</td>
  <td style="text-align: right;">19</td>
  <td style="text-align: right;">19q13.43</td>
  </tr>
  <tr>
  <td style="text-align: left;">A2M</td>
  <td style="text-align: center;">alpha-2-macroglobulin</td>
  <td style="text-align: right;">12</td>
  <td style="text-align: right;">12p13.31</td>
  </tr>
  <tr>
  <td style="text-align: left;">A3M</td>
  <td style="text-align: center;">alpha-3-macroglobulin</td>
  <td style="text-align: right;">12</td>
  <td style="text-align: right;">12p13.33</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>and so on…</p>
  <p>I have made 2 dictionaries, one that matches the gene name (key) with the gene annotation/description (value), and another that matches the gene name (key) with gene chromosome number (value).</p>
  <p>My goal is to make an output file where I take the first table (the one with experiment.1 and experiment.2 as the columns) and append the gene chromosome and gene annotation information to the the table for each appropriate gene using the dictionaries I have created. So essentially, this would lead to an output file in the following format for every gene present in both files. If one gene is not present in a file, the last 2 fields should be NAs (like the second column in the below example)</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Gene.name</th>
  <th style="text-align: center;">Experiment.1</th>
  <th style="text-align: right;">Experiment.2</th>
  <th style="text-align: right;">Gene description</th>
  <th style="text-align: right;">Chromosome number</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A1BG</td>
  <td style="text-align: center;">0.03147</td>
  <td style="text-align: right;">0.05776</td>
  <td style="text-align: right;">alpha-1B-glycoprotein</td>
  <td style="text-align: right;">19</td>
  </tr>
  <tr>
  <td style="text-align: left;">ZNF621</td>
  <td style="text-align: center;">0.091025</td>
  <td style="text-align: right;">0.33516</td>
  <td style="text-align: right;">N/A</td>
  <td style="text-align: right;">N/A</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have set my dictionaries up in the following manner:</p>
  <pre><code>infile = open(&quot;human_gene_annotations.txt&quot;, &quot;rt&quot;)
  
  #separate header
  gene_header = infile.readline()
  
  #gene annotation dict
  gene_annotations = {}
  #use for loop to fill
  for line in infile:
      line = line.rstrip()
      information = line.split(&quot;\t&quot;) 
      gene_annotations[information[0]] = {&quot;Gene Description&quot;: information[1]}
  #close infile 
  infile.close()
  
  
  #open infile again for second dictionary 
  infile = open(&quot;human_gene_annotations.txt&quot;, &quot;rt&quot;)
  #separate header
  gene_header = infile.readline()
  
  #gene chroms dict
  gene_chroms = {}
  #use for loop to fill
  for line in infile:
      line = line.rstrip()
      info_chrom = line.split(&quot;\t&quot;) 
      gene_chroms[info_chrom[0]] = {&quot;Chromosome Number&quot;: info_chrom[2]}
  #close infile 
  infile.close()
  </code></pre>
  <p>I have parsed the data from the first table (the one from the experiments) into lists like so:</p>
  <pre><code>genes = [] 
  exp1values = []
  exp2values = []
  
  for line in infile:
      line = line.rstrip()
      fields = line.split(&quot;\t&quot;) # this will split the line we read by tab, thus by &quot;column&quot;
      genes.append(fields[0])
      exp1values.append(fields[1])
      exp2values.append(fields[2])
  </code></pre>