[Solved] Find the nearest value in Python List [duplicate]


Code:

#Input
l = [2011,2010,2012,2013,2014,2015,2016,2017,2018,2020,2021,2022,2023] 

# Enter year
yr = int(input('Enter year :'))         

#Here I am  creating new list by deducting the input year  
#like 2011-2008 list elemnt will be 3 list ex: [3,2,4..]                              
minus = [abs(i-yr) for i in l]     


#Lastly to find the near year, by searching the min of minus list
l[minus.index(min(minus))] 

Output:

2010

0

solved Find the nearest value in Python List [duplicate]