adjacent differences with a list comprehension

Sean Ross sross at connectmail.carleton.ca
Mon Mar 24 18:54:27 EST 2003


"Greg Ewing (using news.cis.dfn.de)" <me at privacy.net> wrote in message
news:b5o3o8$2bhikr$1 at ID-169208.news.dfncis.de...
> I would suggest:
>
>    [seq[i+1] - seq[i] for i in xrange(len(seq) - 1)]
>
> It's a bit more efficient to boot, since it avoids
> constructing an intermediate list of tuples.

True.
And,
    import operator
    map(operator.sub, seq[1:], seq[:-1])
suffers from the same inefficiency,  but it is faster  ;)

Sean






More information about the Python-list mailing list