I have a list of dates:
dates = ["Jan2016","Feb2016","Mar2016"]
I want to convert these dates to a datetime format i.e Jan2016 = 201601
My first thought was to create a dict of months and their associated numbers and then map that over the list.
months = {"Jan":1,"Feb":2,"Mar":3}
Here is my current code:
dates = ["Jan2016","Feb2016","Mar2016"]
dic = {"Jan":1, "Feb":2,"Mar":3}
month = []
year = []
for date in dates:
month.append(date[:3])
year.append(date[3:])
month = [dic[n] if n in dic else n for n in month]
d = dict(zip(month,year))
print(d)
The output of this code is a dict like so:
{1: '2016', 2: '2016', 3: '2016'}
What is the most pythonic solution to this problem?
['Jan2015', 'Jan2016']?