I have a list with years as strings but there are few missing years which are represented as empty strings.
I am trying to convert those strings to integers and skip the values which can't be converted using list comprehension and try and except clause?
birth_years = ['1993','1994', '' ,'1996', '1997', '', '2000', '2002']
I tried this code but it's not working.
try:
converted_years = [int(year) for year in birth_years]
except ValueError:
pass
required output:
converted_years = ['1993','1994','1996', '1997', '2000', '2002']
try ... except. Not both.def my_f: try: return int(year); except ValueError: passthen in the comprehension[my_f(year) for year in birth_years if my_f(year) ](the pass means the fasly None is returned when the error is encountered, not this isn't the most efficient to call the function twice)