# Streaming Median

DevFeed: [Streaming Median](<https://devfeed.tech/articles/streaming-median-40277.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2012/06/14/streaming-median/>)

Published: 2012-06-14T22:03:55Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Streaming](<https://devfeed.tech/topics/streaming.md>), [Python](<https://devfeed.tech/topics/python.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [generators](<https://devfeed.tech/topics/generators.md>), [iteration](<https://devfeed.tech/topics/iteration.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [approximation](<https://devfeed.tech/tags/approximation.md>), [big-data](<https://devfeed.tech/tags/big-data.md>), [data-analysis](<https://devfeed.tech/tags/data-analysis.md>), [element](<https://devfeed.tech/tags/element.md>), [generators](<https://devfeed.tech/tags/generators.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [infinite](<https://devfeed.tech/tags/infinite.md>), [input](<https://devfeed.tech/tags/input.md>), [iteration](<https://devfeed.tech/tags/iteration.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [median](<https://devfeed.tech/tags/median.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [sequence](<https://devfeed.tech/tags/sequence.md>), [streaming](<https://devfeed.tech/tags/streaming.md>), [streaming-data](<https://devfeed.tech/tags/streaming-data.md>), [yield](<https://devfeed.tech/tags/yield.md>)

## AI overview

This tutorial presents a Python generator that approximates the median of a potentially infinite integer sequence using constant space. The algorithm adjusts its current estimate by one for each input value that is above or below the estimate, and the article discusses how changing stream distributions affect the result.

## Source excerpt

Problem: Compute a reasonable approximation to a "streaming median" of a potentially infinite sequence of integers. Solution: (in Python) def streamingMedian(seq): seq = iter(seq) m = 0 for nextElt in seq: if m > nextElt: m -= 1 elif m < nextElt: m += 1 yield m Discussion: Before we discuss the details of the Python implementation above, we should note a few things. First, because the input sequence is potentially infinite, we can't store any amount of information that is increasing in the length of the sequence.