Dev Helpers
← Back

Pair each price with its previous observation

Pair each price with its previous observation using Python lists and dictionaries.

Open this example in the interactive library

Source code

from itertools import pairwise
quotes = [102, 100, 107, 105]
changes = [{'price': b, 'change': b-a} for a, b in pairwise(quotes)]
print(changes)

Sample output

[{'price': 100, 'change': -2}, {'price': 107, 'change': 7}, {'price': 105, 'change': -2}]