-2

so i wrote a code that shows what is the most repeated value and repeat time in a list. But i cant figure out how can i show the second, third etc repeated value. Would appreciate if you help.

Here is the code:

for i in lst:
    counter = 0
    for k in lst:
        if i == k:
            counter += 1
print("most repeated value is {}, repeated by {} times".format(k, counter))
Kaan Kaya
  • 9
  • 3

1 Answers1

0

A more pythonic way would be to use Counter from collections

from collections import Counter
lst = [1,2,3,1,2,3,4,5,1,1,9,3,2,1,1,3,0,3,4,5]
count = Counter(lst)
# shows n most common values in the list
count.most_common(3)

#OUTPUT
[(1, 6), (3, 5), (2, 3)]

The output shows 1 is the most common with 6 occurrences, 3 is the second most common and so on.

kinshukdua
  • 1,849
  • 1
  • 4
  • 15
  • Thanks for the help but im curious about something else; how can i replace the parantheses and commas in them with something else that i wish? – Kaan Kaya Oct 13 '21 at 13:19
  • If it helped, please select this as the answer. You can iterate over the most common items like this `for i,j in count.most_common(3):` and then print i and j however you want. – kinshukdua Oct 13 '21 at 13:22