Build a Partial Index Mock in Python for Database Filtering

Simulate a partial database index by filtering keys with a predicate, then return a limited mock lookup dictionary.

Easy Python 3.9+ Aug 9, 2026 Database scaling & optimization 12 views 0 copies

Python code

20 lines
Python 3.9+
data = [
    "alpha", "beta", "gamma", "delta", "epsilon",
    "zeta", "eta", "theta", "iota", "kappa"
]

filtered_keys = [item for item in data if len(item) >= 5]

def mock_partial_index(keys, filter_func, limit=3):
    result = {}
    for key in keys:
        if not filter_func(key):
            continue
        result[key] = f"mock://{key}"
        if len(result) >= limit:
            break
    return result

if __name__ == "__main__":
    index = mock_partial_index(filtered_keys, lambda k: k.startswith(("a", "b", "d", "e")))
    print(index)

Output

stdout
{'alpha': 'mock://alpha', 'beta': 'mock://beta', 'delta': 'mock://delta'}

How it works

This function mimics a partial index by applying a filter_func to each key before adding it to the result. It limits entries to limit (default 3) to simulate practical index size constraints. The lambda checks if keys start with common prefixes, and only filtered keys are stored. The mock URL format gives a placeholder for actual indexed lookups.

Common mistakes

  • Forgetting that the limit applies after filtering, not before
  • Assuming the input order is alphabetical when it's the original list order
  • Using `filter()` built-in which returns an iterator, not a dict
  • Not handling empty filtered results gracefully

Variations

  1. Use `itertools.islice` to lazy-limit the filtered sequence
  2. Return a `dict.fromkeys` with the filter applied for one-liner mock

Real-world use cases

  • Simulating a partial index in a test suite before implementing the real database query
  • Prototyping index-aware query behavior in a mock service for unit tests
  • Comparing filtered vs full index lookup performance in a profiling script

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Database scaling & optimization

Related tutorials and quizzes for this topic.