# Python filter list\_models() based on lastModified field

**URL:** <https://discuss.huggingface.co/t/python-filter-list-models-based-on-lastmodified-field/67248>\
**Category:** 🤗Hub\
**Created:** [December 28, 2023, 8:54am UTC](https://discuss.huggingface.co/t/python-filter-list-models-based-on-lastmodified-field/67248 "2023-12-28T08:54:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MKABALAN](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@MKABALAN](https://discuss.huggingface.co/u/MKABALAN)\
**Post date:** [December 28, 2023, 8:54am UTC](https://discuss.huggingface.co/t/python-filter-list-models-based-on-lastmodified-field/67248/1 "2023-12-28T08:54:52Z")

</div>

Hello, I am using hf HUB API to fetch data after calling list\_models().  
I don’t want to fetch data based on limit, I want it based on lastModified field in the model info.  
For example:  
list models that have lastModified \> ‘2023-01-01’  
Is there a way to do it?

---

<div class="post-metadata">

**Author:** ![Wauplin](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/wauplin/32/40815_2.png) [@Wauplin](https://discuss.huggingface.co/u/Wauplin)\
**Post date:** [January 12, 2024, 10:14am UTC](https://discuss.huggingface.co/t/python-filter-list-models-based-on-lastmodified-field/67248/2 "2024-01-12T10:14:22Z")

</div>

Hi @MKABALAN, there is no built-in way to filter like this. However what you can do is to sort the search by `lastModified` and then iterate over the results until you reach the limit you’ve want. Since the results are paginated and lazy-loaded, you won’t have to fetch all models before filtering them. Here is an example to get the number of models that have been updated over the last week:

```python
from datetime import datetime

from huggingface_hub import list_models

recent_models = []
for model in list_models(sort="lastModified", direction=-1):
    if model.last_modified is None or model.last_modified < datetime(2024, 1, 5, tzinfo=model.last_modified.tzinfo):
        break
    recent_models.append(model)

print(len(recent_models))

```

Which outputs `12665`.

---

<div class="post-metadata">

**Author:** ![MKABALAN](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@MKABALAN](https://discuss.huggingface.co/u/MKABALAN)\
**Post date:** [January 13, 2024, 1:45pm UTC](https://discuss.huggingface.co/t/python-filter-list-models-based-on-lastmodified-field/67248/3 "2024-01-13T13:45:24Z")

</div>

This does the trick thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [January 14, 2024, 1:46am UTC](https://discuss.huggingface.co/t/python-filter-list-models-based-on-lastmodified-field/67248/4 "2024-01-14T01:46:08Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
