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.