Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:32:17+00:00 2026-05-26T20:32:17+00:00

I have a Python datetime timestamp and a large dict (index) where keys are

  • 0

I have a Python datetime timestamp and a large dict (index) where keys are timestamps and the values are some other information I’m interested in.

I need to find the datetime (the key) in index that is closest to timestamp, as efficiently as possible.

At the moment I’m doing something like:

for timestamp in timestamps:
    closestTimestamp = min(index,key=lambda datetime : abs(timestamp - datetime))

which works, but takes too long – my index dict has millions of values, and I’m doing the search thousands of times. I’m flexible with data structures and so on – the timestamps are roughly sequential, so that I’m iterating from the first to the last timestamps. Likewise the timestamps in the text file that I load into the dict are sequential.

Any ideas for optimisation would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:32:17+00:00Added an answer on May 26, 2026 at 8:32 pm

    Dictionaries aren’t organized for efficient near miss searches. They are designed for exact matches (using a hash table).

    You may be better-off maintaining a separate, fast-searchable ordered structure.

    A simple way to start off is to use the bisect module for fast O(log N) searches but slower O(n) insertions:

    def nearest(ts):
        # Given a presorted list of timestamps:  s = sorted(index)
        i = bisect_left(s, ts)
        return min(s[max(0, i-1): i+2], key=lambda t: abs(ts - t))
    

    A more sophisticated approach suitable for non-static, dynamically updated dicts, would be to use blist which employs a tree structure for fast O(log N) insertions and lookups. You only need this if the dict is going to change over time.

    If you want to stay with a dictionary based approach, consider a dict-of-lists that clusters entries with nearby timestamps:

     def get_closest_stamp(ts):
          'Speed-up timestamp search by looking only at entries in the same hour'
          hour = round_to_nearest_hour(ts)
          cluster = daydict[hour]         # return a list of entries
          return min(cluster, key=lambda t: abs(ts - t))
    

    Note, for exact results near cluster boundaries, store close-to-the-boundary timestamps in both the primary cluster and the adjacent cluster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python CGI program which prints datetime values. The timestamps are all
I have python dict object with key as datetime.date object and values as tuple
I have some Python code where I need to call upon a dictionary entry
I have a Python datetime.datetime object. What is the best way to subtract one
i have an issue i could use some help with, i have python list
How do you convert a Python time.struct_time object into a datetime.datetime object? I have
I had never worked with the datetime module in Python 2.3, and I have
Trying to figure out datetime module and need some help. I've got a string
I have the following python code: from django.db import models from datetime import datetime
I have a postgresql table with a timestamp field, among other fields. How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.