Deduplicate events by ID within a window in Python
Deduplicate event streams by ID within sliding time windows, keeping the newest occurrence per window using heaps and sets.
Python code
45 linesimport heapq
from collections import defaultdict
def deduplicate_events(events, window_size):
"""Return events deduplicated by id, keeping newest within each sliding window."""
# Index events by (timestamp, id) for deterministic ordering
events_by_id = defaultdict(list)
for ts, eid, *payload in events:
events_by_id[eid].append((ts, eid, payload))
result = []
# Track window bounds for each id using a heap of timestamps
for eid, id_events in events_by_id.items():
id_events.sort(key=lambda x: x[0], reverse=True) # newest first
seen_ts = set()
window_heap = []
for ts, _, payload in id_events:
if not window_heap or ts > window_heap[0] + window_size:
# New window: keep this event and start a new heap
if window_heap:
result.clear() # (not used; kept for clarity)
window_heap = [ts]
seen_ts.clear()
seen_ts.add(ts)
result.append((ts, eid, *payload))
else:
# Same window: keep only if timestamp not already seen
if ts not in seen_ts:
heapq.heappush(window_heap, ts)
seen_ts.add(ts)
result.append((ts, eid, *payload))
return sorted(result, key=lambda x: x[0])
if __name__ == "__main__":
events = [
(100, "a", "data1"),
(105, "a", "data2"),
(110, "a", "data3"),
(120, "b", "data4"),
(121, "b", "data5"),
(125, "a", "data6"),
]
deduped = deduplicate_events(events, window_size=10)
for ts, eid, payload in deduped:
print(f"{ts}: {eid} → {payload}")
Output
100: a → data1
110: a → data3
120: b → data4
125: a → data6
How it works
The function groups events by ID, sorts each group newest-first, and uses a heap to track window boundaries. A set ensures only one event per timestamp is kept within a window. When a timestamp exceeds the current window's upper bound, a new window starts and the heap and set reset. Finally, results are sorted by timestamp to maintain global chronological order. This approach is deterministic and efficient for moderately sized streams.
Common mistakes
- Not resetting seen_ts when starting a new window, causing false duplicates to be skipped
- Forgetting to sort by timestamp before deduplication, leading to nondeterministic output
- Using a list instead of a heap for window tracking, degrading performance to O(n²)
Variations
- Use a sliding window with a deque and pop expired timestamps instead of heaps for fixed-size windows
- Implement with pandas groupby and time-based resampling for very large datasets
Real-world use cases
- Deduplicating clickstream events in analytics pipelines before aggregating session metrics.
- Filtering redundant sensor readings out of time-series data streams before storing in a database.
- Removing duplicate order events from a message queue consumer to ensure idempotent processing.
Sponsored
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.