adjacent differences with a list comprehension

Cliff Wells LogiplexSoftware at earthlink.net
Mon Mar 24 16:46:54 EST 2003


On Mon, 2003-03-24 at 13:31, Phil Schmidt wrote:
> Given a list of numbers, such as:
> 
> L = [2, 5, 8, 3, 9, 1]
> 
> I want to generate a list containing the differences between adjacent
> elements, i.e.,
> 
> Ld = [3, 3, -5, 6, -8]
> 
> I don't see how I can do this (easily/elegantly) with list
> comprehensions.
> 
> Any suggestions? Thanks!

[j - i for i, j in zip(L, L[1:])]


-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308






More information about the Python-list mailing list