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>Another possible solution with comments to help...</p>

<pre><code>import operator

#list
lst = ["abc", "abc", "omg", "what", "abc", "omg"]

#dictionary
countDic = {}

#iterate lst to populate dictionary: {'what': 1, 'abc': 3, 'omg': 2}
for i in lst:
    if i in countDic:
        countDic[i] += 1
    else:
        countDic[i] = 1

#clean list
lst = []

#convert dictionary to an inverse list sorted by value: [('abc', 3), ('omg', 2), ('what', 1)]
sortedLst = sorted(countDic.items(), key=operator.itemgetter(0))

#iterate sorted list to populate list
for k in sortedLst:
    if k[1] != 1:
        lst.append(str(k[1]) + " " + k[0])
    else:
        lst.append(k[0])

#result
print lst
</code></pre>

<p>Output:</p>

<pre><code>['3 abc', '2 omg', 'what']
</code></pre>

python