How can I extract only the day from a list such as this?
date_data = [None, None, None, None, '2014-01-05', '2014-01-06', '2014-01-07', None, None, '2014-01-10']
I’ve tried a few different lambda functions such as:
Date_List = [d for y, m, d in map(lambda x: str(x).split('-'), date_data) if y != None if m != None if d != None]
or
test = [datetime.strptime(i, '%Y-%m-%d') for i in date_data]
test1 = [datetime.strftime(i, '%d') for i in test]
But I cannot get the correct output which is:
date_data = [None, None, None, None, '05', '06', '07', None, None, '10']
Does someone know how to accomplish this?
>Solution :
Based on your initial run though you could do this:
Date_List = [x.split('-')[2] if x else None for x in date_data]
which goes through the list elements and appends a none if the element is a none to begin with and if its an element then you can get the day