How to append characters to a string being used as a python dictionary key (when there are multiple entries related to that string)?
<p>I am pulling out sequence coordinates from the output file produced by HMMER (finds DNA sequences, matching a query, in a genome assembly file).</p>

<p>I create a python dictionary where the key is the source sequence name (a string), and the value is a list comprising the start and end coordinates of the target sequence. However, HMMER often finds multiple matches on a single source sequence (contig/chromosome).</p>

<p>This means that as I add to the dictionary, if I come across multiple matches on a contig, each is overwritten by the following match.</p>

<p>E.g. HMMER finds the following matches:</p>

<blockquote>
  <p>Name    Start    End</p>
  
  <p>4415    16723    17556</p>
  
  <p>127    1290    1145</p>
  
  <p>1263    34900    37834</p>
  
  <p>4415    2073    3899</p>
  
  <p>4415    4580    6004</p>
</blockquote>

<p>But this results in the following dictionary (I want separate entries for each match):</p>

<blockquote>
  <p>{'127': ['1290', '1145'], '1263': ['34900', '37834'], '4415': ['4580', '6004']}</p>
</blockquote>

<p>How can I append a letter to the key so that subsequent matches are unique and do not overwrite the previous ones, i.e. 4415, 4415a, 4415b, and so on?</p>

<pre><code>matches = {}

for each line of HMMER file:
    split the line
    make a list of fields 4 &amp; 5 (the coordinates)
    # at this stage I need a way of checking whether the key (sequenceName)
    # is already in the dictionary (easy), and if it is, appending a letter
    # to sequenceName to make it unique
    matches[sequenceName] = list
</code></pre>

<p>It's not a proper way to go to create different keys while the are equal, instead you can use a list for your values and preserve the coordinates in it, for duplicate keys. You can use <code>collections.defaultdict()</code> for this aim:</p>

<pre><code>&gt;&gt;&gt; coords = [['4415', '16723', '17556'], ['127', '1290', '1145'], ['1263', '34900', '37834'], ['4415', '2073', '3899'], ['4415', '4580', '6004']]
&gt;&gt;&gt; from collections import defaultdict
&gt;&gt;&gt; 
&gt;&gt;&gt; d = defaultdict(list)
&gt;&gt;&gt; 
&gt;&gt;&gt; for i, j, k in coords:
...     d[i].append((j, k))
... 
&gt;&gt;&gt; d
defaultdict(&lt;type 'list'&gt;, {'1263': [('34900', '37834')], '4415': [('16723', '17556'), ('2073', '3899'), ('4580', '6004')], '127': [('1290', '1145')]})
</code></pre>

<p>Besides, the idea of adding a character at the end of the keys in not optimum, because you need to have the count of keys always and you are not aware of this number so you have to generate new suffix.</p>

<p>But as an alternative if you only use the count of the keys you can create different ones by preserving the keys in a <code>Counter()</code> object and adding the count at the trailing of the key:</p>

<pre><code>&gt;&gt;&gt; from collections import Counter
&gt;&gt;&gt; d = {}
&gt;&gt;&gt; c = Counter()
&gt;&gt;&gt; for i, j, k in coords:
...     c.update((i,))
...     d["{}_{}".format(i, c[i])] = (j, k)
... 
&gt;&gt;&gt; d
{'4415_1': ('16723', '17556'), '4415_3': ('4580', '6004'), '4415_2': ('2073', '3899'), '127_1': ('1290', '1145'), '1263_1': ('34900', '37834')}
</code></pre>

python dictionary key unique