from operator import itemgetter


def freq_table(column, order_by_desc=False):
    """
    Generates a frequency table from the values on a column and stores it in an dictionary.
    :param list column: the list containing all the items that will generates the frequency table
    :param bool order_by_desc: if the table will be sorted descend by items values (default=False)
    :return: a dictionary with column items as keys and a list containing absolute and relative frequencies
    """
    # initialize an empty dictionary
    frequency_table = {}

    # calculate the total of items in table
    total = len(column)

    # loop through column rows
    for row in column:
        if row in frequency_table:
            # update the values inside 'row' list:
            # increment absolute frequency by 1
            frequency_table[row][0] += 1
            # divides absolute frequency by the totals
            frequency_table[row][1] = frequency_table[row][0] / total
        else:
            # creates an item in dictionary with 'row' as key and of list type
            frequency_table[row] = []
            # the first item of list is absolute frequency. set as 1 (first occurrence)
            frequency_table[row].append(1)
            # the second item is relative frequency, i.e., absolute / total
            frequency_table[row].append(1 / total)

    if order_by_desc:
        return sorted(frequency_table.items(), key=itemgetter(1), reverse=True)
    else:
        return frequency_table