-5

How can I print the most common element of a list without importing a library?

l=[1,2,3,4,4,4]

So I want the output to be 4.

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
  • I'm brand-new here,i've not get used to this type of asking.I'm sorry.Could you please answer my question? – vasilistheod Jan 12 '17 at 11:50
  • 2
    No. Because **this isn't a code-writing service**. Learn [ask], and actually put in some effort yourself before dumping it on SO. – jonrsharpe Jan 12 '17 at 11:57
  • 2
    Possible duplicate of [Python most common element in a list](http://stackoverflow.com/questions/1518522/python-most-common-element-in-a-list) – CaptainObvious Jan 12 '17 at 11:59

1 Answers1

0
lst=[1,2,2,2,3,3,4,4,5,6]
from collections import Counter
Counter(lst).most_common(1)[0]

Counter(lst) returns a dict of element-occurence pairs. most_common(n) returns the n most common elements from the dict, along with the number of occurences.

Anomitra
  • 1,006
  • 14
  • 28