It should be noted that none of these solutions works for generators. For this, see Glenn Maynards Solution.
use zip for small lists:
for current, last in zip(entries[1:], entries):
diff = current - last
This makes a copy of the list (and a list of tuples from both copies of the list), so it is useful to use itertools to process large lists
import itertools as it
items = it.izip(it.islice(entries, 1, None), entries)
for current, last in items:
diff = current - last
This will avoid both copying a list copy and compiling a list of tuples.
Another way to do this without making a copy is
entry_iter = iter(entries)
entry_iter.next()
for i, entry in enumerate(entry_iter):
diff = entry - entries[i]
:
for i in xrange(len(entries) - 1):
diff = entries[i+1] - entries[i]
, entries . enumerate . 0 , .
, , , .
diffs = (current - last for current, last in
it.izip(it.islice(entries, 1, None), entries))