Open In App

Index Specific Cyclic Iteration in List – Python

Last Updated : 17 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with lists in Python, there may be cases where we need to iterate over a list starting from a specific index in a cyclic manner. For example, given the list [10, 20, 30, 40, 50], we may want to start iterating from index 2 (i.e., 30) and continue iterating in a cyclic manner. There are multiple ways to accomplish this task, which we will explore below.

Using cycle() with range()

This method uses itertools.cycle() to create an infinite iterator over the list and range() to limit the number of iterations. It starts iterating from a given index and cycles through the list.

Python
from itertools import cycle
a = [10, 20, 30, 40, 50]
# Starting index
start = 2

# Create a cyclic iterator using cycle
cyclic_iter = cycle(a)
# Use range to limit the number of iterations
for i in range(start, start + len(a)):
    print(next(cyclic_iter))

Output
10
20
30
40
50

Explanation:

  • cycle() creates an infinite iterator that will keep repeating the list.
  • We use range(start, start + len(a)) to iterate exactly len(a) times, starting from the start index.
  • next(cyclic_iter) used to retrieve the next element of the cyclic iterator.

Let’s explore some more methods and see how we can index specific cyclic iteration in list.

Using while loop with modulus operator

In this method, we use a while loop to iterate through the list cyclically by using the modulus operator to wrap around once we reach the end of the list.

Python
a = [10, 20, 30, 40, 50]
# Starting index
start = 2
# Length of the list
n = len(a)
# Counter to track the number of iterations
i = start

# While loop to iterate cyclically
while True:
    print(a[i % n])  
    i += 1
    if i == start + n:
        break

Output
30
40
50
10
20

Explanation:

  • We start iterating from the specified index and use the modulus operator to handle the wraparound when the index exceeds the length of the list.
  • The loop terminates after iterating through all elements.

Using list slicing and concatenation

In this method, we use list slicing to split the list into two parts: one from the start index and the other from the beginning of the list. We concatenate these parts and then iterate through the result.

Python
a = [10, 20, 30, 40, 50]
# Starting index
start = 2

# List slicing and concatenation for cyclic iteration
a = a[start:] + a[:start]
for item in a:
    print(item)

Output
30
40
50
10
20

Explanation:

  • We slice the list into two parts and concatenate them so that the iteration starts from the given index.
  • The loop then goes through the modified list.

Using for loop

This method uses a for loop to iterate over the list while manually adjusting the index using the modulus operator to achieve cyclic iteration.

Python
a = [10, 20, 30, 40, 50]
# Starting index
start = 2

# Using for loop to manually adjust the index
for i in range(start, start + len(a)):
    print(a[i % len(a)])

Output
30
40
50
10
20

Explanation:

  • The range()function iterates over the list starting from the specified index.
  • The modulus operator ensures that the index wraps around to the beginning of the list when needed.


Next Article

Similar Reads