Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

acces previous index in cycle python

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
Comment

PREVIOUS NEXT
Code Example
Python :: how to plot a single cluster 
Python :: intersection of 3 array in O(n) python 
Python :: python wrapper function 
Python :: combine column in csv python pandas 
Python :: python closing socket good way 
Python :: enumerate() 
Python :: python function as argument 
Python :: how to use python to download files from the interent 
Python :: pytorch inverse 
Python :: matplotlib custom legends 
Python :: python decomposition facteur premier 
Python :: email confirmation django 
Python :: python os 
Python :: trim all new rows string python 
Python :: python slice last 2 items of list 
Python :: download google drive link collab 
Python :: how to remove axis in matplotlib 
Python :: how to append to an empty dataframe pandas 
Python :: get last save id django model 
Python :: Python __floordiv__ magic method 
Python :: use python to download youtube playlist mp3 
Python :: How can i restrict letters after a number in an input in Python 
Python :: godot remove node from group 
Python :: empty array numpy python 
Python :: python triangular number 
Python :: print f python 
Python :: histogram relative frequency 
Python :: decision tree 
Python :: pandas unstring list 
Python :: importing a python file from another folder 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =