Index Specific Cyclic Iteration in List - Python
Last Updated :
11 Jul, 2025
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))
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
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)
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)])
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.
Similar Reads
Python | Triplet iteration in List List iteration is common in programming, but sometimes one requires to print the elements in consecutive triplets. This particular problem is quite common and having a solution to it always turns out to be handy. Lets discuss certain way in which this problem can be solved. Method #1 : Using list co
6 min read
How to Find Index of Item in Python List To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether weâre checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it
2 min read
Python - Find Index containing String in List In this article, we will explore how to find the index of a string in a list in Python. It involves identifying the position where a specific string appears within the list.Using index()index() method in Python is used to find the position of a specific string in a list. It returns the index of the
2 min read
Python - Index Value repetition in List Given a list of elements, The task is to write a Python program to repeat each index value as per the value in that index. Input : test_list = [3, 0, 4, 2] Output : [0, 0, 0, 2, 2, 2, 2, 3, 3] Explanation : 0 is repeated 3 times as its index value is 3. Input : test_list = [3, 4, 2] Output : [0, 0,
7 min read
Python - Incremental and Cyclic Repetition of List Elements Sometimes, while working with Python lists, we can have a problem in which we need to repeat elements K times. But we can have variations in this and have to repeat elements in cyclic and incremental way. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + enumer
4 min read
Accessing index and value in Python list We are given a list, and our task is to access both the index and value of each element in the list using Python. For example, using enumerate(list) in a loop like for index, value in enumerate(list) allows us to access both the index and the value together.Using enumerate() enumerate() is preferred
2 min read