Sometimes, we want to find differences between elements of a list with Python.
In this article, we’ll look at how to find differences between elements of a list with Python.
How to find differences between elements of a list with Python?
To find differences between elements of a list with Python, we can use the zip.
For instance, we write
t = [1, 3, 6]
diffs = [j-i for i, j in zip(t[:-1], t[1:])]
to call zip with t[:-1] and t[1:] to zip the items in t that are next to each other together.
Then we subtract them with j - i.
And we put the differences of the values in a list.
Conclusion
To find differences between elements of a list with Python, we can use the zip.