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

How to find recent values inside a list?

I have a list of dates with a format %y-%m-%d and a single value. I need to find all the dates that are more recent than value

dates = ['22-02-10','22-02-11','22-02-12','22-02-13','22-02-14','22-02-15']
value = '22-02-12'

the output should be false,false,false,true,true,true.
How can I perform this in a fast way without a for loop?

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

>Solution :

You can use numpy to let the underlying c implementation do the looping (which is way faster then pure python)

import numpy as np
dates = ['22-02-10','22-02-11','22-02-12','22-02-13','22-02-14','22-02-15']
value = '22-02-12'
dates = np.array(dates, dtype='datetime64')
value = np.array(value, dtype='datetime64')
print(dates > value)
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