After reading this question, it jumped to me that many said that the solution to the problem is over-engineered / overkill.

Here I solve the same problem in Python in a more compact way. I think that this solution is compact enough. If you still think that this is overkill for the problem, please tell me. Any kind of other code review is welcome too.

Item list analyzer

Given a file with an element in each line,
for each item this programme will print the number of times
it appears in the text file. It will also sort the items
from the most frequent to the least frequent.

        spam \n spam \n eggs \n spam \n foo \n baz \n eggs
        # "\n" should be actual newlines not the "\n" string

        ('spam', 3)
        ('eggs', 2)
        ('baz', 1)
        ('foo', 1)

import operator

FILENAME = "cars.txt"

def return_frequncy_dict(lst):
    Given a list as input returns a 
    dict containg the items as keys and
    the number of occurencies of each item as

    >>> return_frequncy_dict(["a","b","c","b","b","a"])
    {'a': 2, 'c': 1, 'b': 3}
    return {i:lst.count(i) for i in lst}

def sort_by_value(dict_):
    As the frequency is the value, to sort by frequency we
    must sort by value. Standard sorting goes from the smallest
    to the biggest, we want the reverse of it.

    Credit for this function goes to
    return reversed(sorted(dict_.items(), key=operator.itemgetter(1)))

if __name__ == "__main__":
    with open(FILENAME,"r") as f:
        data = f.read()

    item_list = data.splitlines()
    item_list = [item.lower() for item in item_list]
    item_list = [item.strip() for item in item_list]

    for j in sort_by_value(return_frequncy_dict(item_list)):

I'm sure that someone who knows more Python than me will be able to provide more feedback, but I see a couple of issues.

The first is that the core of this functionality is implemented by collections.Counter. We can just write this:

def frequency(lst):
    counter = Counter(lst)
    return reversed(sorted(counter.items(), key=lambda x:x[1]))

The second issue is with these lines:

item_list = [item.lower() for item in item_list]
item_list = [item.strip() for item in item_list]

We can just write

item_list = [item.lower().strip() for item in item_list]

Or as @janos suggested,

item_list = [item.lower().strip() for item in data.splitlines()]
reversed(sorted(dict_.items(), key=operator.itemgetter(1)))

can be written as a single function call:

sorted(dict_.items(), key=operator.itemgetter(1), reverse=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.