access previous value python loop

Solutions on MaxInterview for access previous value python loop by the best coders in the world

showing results for - "access previous value python loop"
Christian
13 Sep 2019
1a_list = [1,2,3,4,5]
2
3for index, elem in enumerate(a_list):
4    if (index+1 < len(a_list) and index - 1 >= 0):
5
6        prev_el = str(a_list[index-1])
7        curr_el = str(elem)
8        next_el = str(a_list[index+1])
9
10        print(prev_el, curr_el, next_el)
11OUTPUT
121 2 3
132 3 4
143 4 5