I have a dictionary with many sorted dates. How could I write a loop in Python that checks if a certain date is in the dictionary and if not, it returns the closest date available? I want it to work that if after subtracting one day to the date, it checks again if now it exists in the dictionary and if not, it subtracts again until it finds a existing date. 

Thanks in advance

```py
from datetime import timedelta

def function(date):
    if date not in dictio:
        date -= timedelta(days=1)
    return date

```