Python | Index specific cyclic iteration in list
The problem of cyclic iteration is quite common, but sometimes, we come through the issue in which we require to process the list in a way in which it is cyclically iterated to starting from a specific index. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using % operator + loop
The % operator can be used to cycle the out-of-bound index value to begin from the beginning of list to form a cycle and hence help in the cyclic iteration.
Python3
# Python3 code to demonstrate # cyclic iteration in list # using % operator and loop # initializing tuple list test_list = [ 5 , 4 , 2 , 3 , 7 ] # printing original list print ( "The original list is : " + str (test_list)) # starting index K = 3 # using % operator and loop # cyclic iteration in list res = [] for i in range ( len (test_list)): res.append(test_list[K % len (test_list)]) K = K + 1 # printing result print ( "The cycled list is : " + str (res)) |
The original list is : [5, 4, 2, 3, 7] The cycled list is : [3, 7, 5, 4, 2]
Time Complexity: O(n), where n is the length of the given list
Auxiliary Space: O(n)
Method #2 : Using itertools.cycle() + itertools.islice() + itertools.dropwhile()
The itertools library has built in functions that can help achieve to the solution of this particular problem. The cycle function performs the cycling part, dropwhile function brings the cycle to begin of list and islice function specifies the cycle size.
Python3
# Python3 code to demonstrate # cyclic iteration in list using itertools from itertools import cycle, islice, dropwhile # initializing tuple list test_list = [ 5 , 4 , 2 , 3 , 7 ] # printing original list print ( "The original list is : " + str (test_list)) # starting index K = 3 # using itertools methods for # cyclic iteration in list cycling = cycle(test_list) skipping = dropwhile( lambda x: x ! = K, cycling) slicing = islice(skipping, None , len (test_list)) slicing = list (slicing) # printing result print ( "The cycled list is : " + str (slicing)) |
The original list is : [5, 4, 2, 3, 7] The cycled list is : [3, 7, 5, 4, 2]
Time complexity: O(n), where n is the length of the original list
Auxiliary space: O(k), where k is the number of elements in the new cycled list.
Method #3 : Using slice() method
The slice method is used to create slices of the original list, starting at index K and continuing to the end, and starting at the beginning and continuing up to index K-1. These slices are then concatenated to create a new list that is cyclically shifted to the right by K indices.
Python3
# initializing list test_list = [ 5 , 4 , 2 , 3 , 7 ] # printing original list print ( "The original list is : " + str (test_list)) # starting index K = 3 # cyclic iteration in list # using slice method res = test_list[ slice (K, len (test_list))] + test_list[ slice ( 0 , K)] # printing result print ( "The cycled list is : " + str (res)) |
The original list is : [5, 4, 2, 3, 7] The cycled list is : [3, 7, 5, 4, 2]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4 : Using slicing and extend() method
Python3
# Python3 code to demonstrate # cyclic iteration in list # initializing tuple list test_list = [ 5 , 4 , 2 , 3 , 7 ] # printing original list print ( "The original list is : " + str (test_list)) # starting index K = 3 # cyclic iteration in list a = test_list[:K] b = test_list[K:] b.extend(a) # printing result print ( "The cycled list is : " + str (b)) |
The original list is : [5, 4, 2, 3, 7] The cycled list is : [3, 7, 5, 4, 2]
Time Complexity : O(N)
Auxiliary Space : O(N)
Using deque.rotate():
Approach:
The deque class in Python has a rotate() method that can be used to rotate the elements of a list. We can use this method to “cycle” the desired index to the beginning of the list.
- Import the deque class from the collections module: from collections import deque
- Define the original list: original_list = [5, 4, 2, 3, 7]
- Create a deque object from the original list: cycled_list = deque(original_list)
- Rotate the elements of the deque by a negative value (-3): cycled_list.rotate(-3)
- Iterate over the elements of the cyclic list using the index, and print each element:
Python3
from collections import deque original_list = [ 5 , 4 , 2 , 3 , 7 ] cycled_list = deque(original_list) cycled_list.rotate( - 3 ) for i in range ( len (cycled_list)): print (cycled_list[i]) |
3 7 5 4 2
Time complexity: O(n)
Space complexity: O(n)
Method 6: using list comprehension and modulo operator
Python3
# Python3 code to demonstrate # cyclic iteration in list # using list comprehension and modulo operator # initializing tuple list test_list = [ 5 , 4 , 2 , 3 , 7 ] # printing original list print ( "The original list is : " + str (test_list)) # starting index K = 3 # using list comprehension and modulo operator # cyclic iteration in list res = [test_list[(K + i) % len (test_list)] for i in range ( len (test_list))] # printing result print ( "The cycled list is : " + str (res)) |
The original list is : [5, 4, 2, 3, 7] The cycled list is : [3, 7, 5, 4, 2]
Time complexity: O(n)
Auxiliary space: O(n) since a new list is created to store the cycled list.
Method 7: using itertools.cycle() and itertools.islice()
- Creates an iterator from the input list using itertools.cycle(), which infinitely repeats the input list.
- It then uses itertools.islice() to select a slice of the iterator starting at index K and ending at K+len(test_list) (exclusive).
- Finally, it converts the resulting slice back to a list using list().
Python3
import itertools # initializing list test_list = [ 5 , 4 , 2 , 3 , 7 ] # printing original list print ( "The original list is:" , test_list) # starting index K = 3 # using itertools.cycle() and itertools.islice() # cyclic iteration in list res = list (itertools.islice(itertools.cycle(test_list), K, K + len (test_list))) # printing result print ( "The cycled list is:" , res) |
The original list is: [5, 4, 2, 3, 7] The cycled list is: [3, 7, 5, 4, 2]
Time complexity: O(n)
Auxiliary space: O(n) (for the output list)
Please Login to comment...