Is there a way to "peek" at the next iterable value without modifying an iterator?
I want to write a for loop over a list that touches the "current" value of the list and the "next" value of the list simultaneously, and I'm wondering if there's a streamlined way to do this with Python iterators.
E.g. in C++ I could write something like
std::list<int>::const_iterator iterator;
for (iterator = myList.begin(); (iterator+1) != myList.end(); iterator++) {
compute = *iterator + *(iterator + 1);
}
because the value of the iterator only changes when explicitly incremented. The next() function in Python always increments the iterator. The only solutions I'm coming up with are things like having two iterators, which seems clunky. Is there a nice simple for-loop solution that looks something like:
for x in myList[0:-2]:
compute = x + x.peeknext();