What if you need to combine the current value with the previous or next value?

Python loop with previous and next value. Is that possible?

A for loop in python makes each element in the list accessible through the loop variable. But each iteration, only the current value is available. What if you need to combine the current value with the previous or next value? In this video, you learn how to create a generator function that loops over a list and gives you the previous, current and next value each iteration.

Written by Loek van den Ouweland on 2024-02-22.
Questions regarding this artice? You can send them to the address below.