Count duplicates in list and assign the sum into list
<p>I have a list with duplicate strings:</p>

<pre><code>lst = ["abc", "abc", "omg", "what", "abc", "omg"]
</code></pre>

<p>and I would like to produce:</p>

<pre><code>lst = ["3 abc", "2 omg", "what"]
</code></pre>

<p>so basically count duplicates, remove duplicates and add the sum to the beginning of the string.</p>

<p>This is how I do it right now:</p>

<pre><code>from collections import Counter
list2=[]
for i in lst:
  y = dict(Counter(i))
  have = list(accumulate(y.items())) # creating [("omg", 3), ...]

  for tpl in have: #
    join_list = []
    if tpl[1] &gt; 1:
      join_list.append(str(tpl[1])+" "+tpl[0])
    else:
      join_list.append(tpl[0])
  list2.append(', '.join(join_list))
</code></pre>

<p>Is there a easier way to obtain the desired result in python?</p>

<p>You've properly used the Counter type to accumulate the needed values.  Now, it's just a matter of a more Pythonic way to generate the results.  Most of all, pull the initialization out of the loop, or you'll lose all but the last entry.</p>

<pre><code>list2 = []
for tpl in have:
    count = "" if tpl[1] == 0 else str(tpl[1])+" "
    list2.append(count + tpl[0])
</code></pre>

<p>Now, to throw all of that into a list comprehension:</p>

<pre><code>list2 = [ ("" if tpl[1] == 0 else str(tpl[1])+" ") + tpl[0] \
          for tpl in have]
</code></pre>

python