Nested for-loop to create list of lists - Python I have posted the below also at stackoverflow in this thread, but a poster there recommended I post this here.  Thanks for any suggestions.

I have the below code, and as someone new to Python, I was hoping for suggestions on making it more Pythonic.  Posters in this thread have provided very helpful numpy and list comprehension solutions; however, I'd like to improve my more basic nested loop solution solely for the sake of becoming better with basic Python tasks (please do let me know if I should have posted this in the prior thread instead of starting a new semi-duplicate...apologies if that's what I should have done).

Here's the current code (which works as desired):

sales_data = [[201703, 'Bob', 3000], [201703, 'Sarah', 6000], [201703, 
'Jim', 9000], [201704, 'Bob', 8000], [201704, 'Sarah', 7000], [201704, 
'Jim', 12000], [201705, 'Bob', 15000], [201705, 'Sarah', 14000], [201705, 
'Jim', 8000], [201706, 'Bob', 10000], [201706, 'Sarah', 18000]]

sorted_sales_data = sorted(sales_data, key=lambda x: -x[2])
date_list = []
sales_ranks = []

for i in sales_data:
    date_list.append(i[0])

sorted_dates = sorted(set(date_list), reverse=True)

for i in sorted_dates:
    tmp_lst = []
    tmp_lst.append(i)
    for j in sorted_sales_data:
        if j[0] == i:
            tmp_lst.append(j[1])
    sales_ranks.append(tmp_lst)

print(sales_ranks)


Any suggestions on making this more Pythonic (other than numpy or list comprehension solutions) would be greatly appreciated.  I am using Python 3.6.
Using a pure Python approach, this is exactly the job for itertools.groupby. But for it to work efficiently, you need to sort your data by the same key you will group them by (i.e. the date). Since your first sort also sort by the third field, you need to sort your list by multiple attributes

I would also build a generator to help reuse code and separate the core of your computation from the testing code using if __name__ == '__main__'. Something along the lines of:

from operator import itemgetter
import itertools


def generate_ranks(sales_data):
    sorted_data = sorted(sales_data, key=itemgetter(0, 2), reverse=True)
    for date, group in itertools.groupby(sorted_data, key=itemgetter(0)):
        yield [date] + [entry[1] for entry in group]


if __name__ == '__main__':
    sales_data = [
        [201703, 'Bob', 3000],
        [201703, 'Sarah', 6000],
        [201703, 'Jim', 9000],
        [201704, 'Bob', 8000],
        [201704, 'Sarah', 7000],
        [201704, 'Jim', 12000],
        [201705, 'Bob', 15000],
        [201705, 'Sarah', 14000],
        [201705, 'Jim', 8000],
        [201706, 'Bob', 10000],
        [201706, 'Sarah', 18000],
    ]
    sales_ranks = list(generate_ranks(sales_data))
    print(sales_ranks)