Python Index specific cyclic iteration in list

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to write a program that cyclically iterates a list from the given Let's see the steps to solve the problem

  • Initialize the list and index.
  • Find the length of the list using len.
  • Iterate over the list using the length.
  • Find the index of the element using index % length.
  • Print the element.
  • Increment the index.

It's a simple loop iteration. You can write it without any trouble. Let's see the code.


 Live Demo

# initializing the list and index
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
start_index = 5
# finding the length
length = len(alphabets)
# iterating over the list
for i in range(length):
   # finding the index of the current element
   element_index = start_index % length
   # printing the element
   print(alphabets[element_index], end=' ')
# incrementing the index for the next element
start_index += 1


If you run the above code, then you will get the following result.

f g h a b c d e


If you have any queries regarding the tutorial, mention them in the comment section.

Published on 07-Jul-2020 12:18:31