Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract day from date string in a list

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading