Answers for "access previous value python loop"

0

access previous value python loop

a_list = [1,2,3,4,5]

for index, elem in enumerate(a_list):
    if (index+1 < len(a_list) and index - 1 >= 0):

        prev_el = str(a_list[index-1])
        curr_el = str(elem)
        next_el = str(a_list[index+1])

        print(prev_el, curr_el, next_el)
OUTPUT
1 2 3
2 3 4
3 4 5
Posted by: Guest on September-03-2021

Code answers related to "access previous value python loop"

Python Answers by Framework

Browse Popular Code Answers by Language