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>It seems you are needlessly complicating things. Here is a very Pythonic approach:</p>

<pre><code>&gt;&gt;&gt; import collections
&gt;&gt;&gt; class OrderedCounter(collections.Counter, collections.OrderedDict):
...   pass
... 
&gt;&gt;&gt; lst = ["abc", "abc", "omg", "what", "abc", "omg"]
&gt;&gt;&gt; counts = OrderedCounter(lst)
&gt;&gt;&gt; counts
OrderedCounter({'abc': 3, 'omg': 2, 'what': 1})
&gt;&gt;&gt; ["{} {}".format(v,k) if v &gt; 1 else k for k,v in counts.items()]
['3 abc', '2 omg', 'what']
&gt;&gt;&gt; 
</code></pre>

python