-2

I want to produce print the most listed item in a list

for example,

aList = ["SE","CpE","SE","CS","CS","SE"]

and result should be

aList = ["SE"]

which is the most listed item. Please Help!

user2848250
  • 35
  • 1
  • 1
  • 3

2 Answers2

2
from collections import Counter

a = Counter(aList)
a.most_common()[0]

http://docs.python.org/2/library/collections.html#collections.Counter

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
2

There are lots of such questions, but anyway.

You can do this using stdlib's collections.Counter:

from collections import Counter

a = Counter(aList)
a.most_common()[0]

Or you can write your own class/function:

def count(list):
    items = {}
    for item in list:
        if item not in items:
            items[item] = 0
        items[item] += 1
    return items

>>> count(["SE","CpE","SE","CS","CS","SE"])
{"SE": 3,...}
JadedTuna
  • 1,783
  • 2
  • 18
  • 32