Home > Mobile >  Python Program to extract year part from a list of dates
Python Program to extract year part from a list of dates

Time:11-23

This is the list of dates I have been asked to extract the year part from this list ['22-7-1997','1-12-1996','17-06-1992','10-09-1999']

CodePudding user response:

Simple example using datetime module and a list comprehension:

>>> import datetime as dt
>>> dates = ['22-7-1997','1-12-1996','17-06-1992','10-09-1999']
>>> [dt.datetime.strptime(date_string, "%d-%m-%Y").year for date_string in dates]
[1997, 1996, 1992, 1999]

CodePudding user response:

datesList = ['17-12-1997','22-04-2011','01-05-1993','19-06-2020']

year = [x.split('-')[2] for x in datesList] print('Years are: ',year)

  • Related