Xavier
Xavier

Reputation: 45

How to correspond a list value to another value in another list in python

My current line of code is shown below:

year = [1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016]
intake = [26242.5,27906.7,29192.7,30407.5,1564.9,2313.5,2989.4,3668.6,22082.6,29964.2,30481.6,35981.7,39597.6,38659.6, 34325.9,6913.5,35780.6,39111.5,37116.5,35385.5]
outtake = [14311.4,12135.1,3381.0,2186.8,22732.6,21519.8,19932.3,18903.2,24347.6,3307.0,3261.5,34668.1,43695.2,36286.6,26648.6,34325.9,36913.5,26580.6,27679.5,28643.0]

I want to find the 3 lowest values in 'outtake' list and label as variables {val1}, {val2}, {val3} which corresponds with the same row[] in the 'year' list as variables {yr1}, {yr2}, {yr3} respectively.


The desired output shld be a print message:

The 3 lowest outtake values are {val1}, {val2}, {val3} at year {yr1}, {yr2}, {yr3} respectively.

How should I go about doing it?

Upvotes: 0

Views: 41

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32244

You can use zip to iterate over two lists at the same time, sorted to sort them (by the first element) and slice notation ([start:end]) to get the first three

sorted(zip(outtake, year))[:3]

Upvotes: 1

Related Questions