programming

Iterate with a tuple and multiple defaulted previous and next elements

In general pythons iterators are a nice way to iterate fast and source-code-clean over iterable. Sadly you can always only iterate over a single element. In general they are not really flexible (for example an iterator can not go back).

Sometimes you need to iterate not only with a single element but rather with a tuple of elements; therefore multiple previous and next elements to your current. There is always the problem what to do with your previous and next element at the borders or on which place of your tuple the iterator has to start and and end. Often calculations with the index are used, but this gets fast very ugly since you have maybe multiple border conditions and not every python iterable has also an index.

 
 
 

User login