[Answered ]-Django: get top tags?

1👍

Actually, if, for example, the tags are applied to a model called Joke, you can simply do this:

tags = Joke.tags.most_common()

And it works just fine.

1👍

Assuming that the tags are stored in a list:

sorted(lst, key=lst.count, reverse=True)

collections.Counter also works and has the advantage of being O(n)

counts = collections.Counter(lst)
new_list = sorted(lst, key=lambda x: -counts[x])

Leave a comment