How to compare and lookup one list with another list?


I have 2 lists – one holds `int values and the other string. I am iterating through the list to find the highest value in it and once I do I want to lookup the corresponding name for that highest value. I am reading my data from a csv file that has 2 columns: month and amount.

monthSales = []
totalSales = []

for col in sale:
print('Month: ', monthSales)
print('Sales: ', totalSales)

this gives the following output

Month:  ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
Sales:  ['6226', '1521', '1842', '2051', '1728', '2138', '7479', '4434', '3615', '5472', '7224', '1812']

I can get the highest and lowest values using max and min but I want to be able to extract the associated month with it and display that along with the value.

so for example using above: highest sales is 7479 which is in month July. so how do i display both this values?

any help appreciated

I have managed to get max and min sales but just need guidance on how to do a secondary lookup for the corresponding list associated with the sales list

Asked By: Sashraf



You could just make one list of tuples, and then call max or min with the key argument to use the sales number:

sales = [(col['month'], int(col['sales'])) for col in sale]
print(max(sales, key=lambda x: x[1]))
print(min(sales, key=lambda x: x[1]))
Answered By: jprebys
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.