There are several image files in a directory on which I need to iterate through, where need to access the next iteration value in the current iteration.
img_files_list = os.listdir(/path/to/directory)
new_list = []
for index, img in enumerate(img_files_list):
if img in new_list:
continue
new_list.extend([img, img_files_list[index + 1], img_files_list[index + 2]])
print(img)
print(img_files_list[index + 1])
print(img_files_list[index + 2])
I need to iterate over all the items of the img_files_list but when reached at the end of the loop need to properly come out of loop without index out of range exception. Anyone, please point out where do I missed the logic.