Open In App

Python | Alternate Cycling in list

Sometimes, while working with a Python list, we can have a problem in which we need to perform the access/printing of list in different ways. In some variations, there might be a need for printing the list in an alternate cyclic way, i.e printing elements from front and read alternatively. This is a popular problem in-school programming. Let’s discuss a certain way in which this task can be performed. 

Method 1: Using reversed() + islice() + iter() + cycle() + next() + list comprehension The combination of above functions can be employed to perform this task. In this, reversed() and iter() are used to create iterators of reversed and normal sequence list respectively, cycle() performs the task of alternate access. The islice() performs the task of extracting the elements and construct to new list. The next() performs the task of accessing elements. Code : 






# Python3 code to demonstrate working of
# Alternate Cycling in list
# using reversed() + islice() + iter() + cycle() + next() + list comprehension
from itertools import islice, cycle
 
# initialize list
test_list = [5, 6, 8, 9, 10, 21, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Alternate Cycling in list
# using reversed() + islice() + iter() + cycle() + next() + list comprehension
res = [next(i) for i in islice(cycle((iter(test_list),
                                     reversed(test_list))), len(test_list))]
 
# printing result
print("Alternate Cyclic iteration is : " + str(res))

Output : 
The original list is : [5, 6, 8, 9, 10, 21, 3]
Alternate Cyclic iteration is : [5, 3, 6, 21, 8, 10, 9]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 



Method #2 : Using for loop

This method uses two pointers i and j to traverse the original list from both ends simultaneously, and creates a new list result to store the alternate cyclic iteration of the original list. We iterate through the length of the original list using a loop for k in range(n), and use the modulus operator % to determine whether the current index k is even or odd. If it is even, we take the next element from the left end of the original list using lst[i] and store it in the result list at the current index k, and increment the left pointer i. If it is odd, we take the next element from the right end of the original list using lst[j] and store it in the result list at the current index k, and decrement the right pointer j. Finally, we print the original list and the alternate cyclic iteration list.




lst = [5, 6, 8, 9, 10, 21, 3]
n = len(lst)
result = [0] * n
i = 0
j = n - 1
for k in range(n):
    if k % 2 == 0:
        result[k] = lst[i]
        i += 1
    else:
        result[k] = lst[j]
        j -= 1
print("Original list:", lst)
print("Alternate cyclic iteration:", result)

Output
Original list: [5, 6, 8, 9, 10, 21, 3]
Alternate cyclic iteration: [5, 3, 6, 21, 8, 10, 9]

Time complexity: O(n)
Auxiliary Space: O(n)

Method#3: Using the deque class and itertools module:

Algorithm :

1. Create an empty list result to store the result of the iteration.
2. Create two variables i and j, initialized to 0 and len(lst) – 1, respectively.
3. Iterate over the indices k of the input list lst, from 0 to len(lst) – 1.
4.If k is even, append lst[i] to the result list, and increment i.
5.If k is odd, append lst[j] to the result list, and decrement j.
6. When the iteration is complete, return the result list.




# Using the deque class
 
from collections import deque
 
lst = [5, 6, 8, 9, 10, 21, 3]
d = deque(lst)
 
result = []
while d:
    result.append(d.popleft())
    if d:
        result.append(d.pop())
 
print("Original list:", lst)
print("Alternate cyclic iteration:", result)
#This code is contributed by Jyothi pinjala.

Output
Original list: [5, 6, 8, 9, 10, 21, 3]
Alternate cyclic iteration: [5, 3, 6, 21, 8, 10, 9]

The time complexity: O(n), where n is the length of the input list. This is because the algorithm iterates over each element of the input list exactly once, and performs constant-time operations on each element.
The space complexity: O(n), because the deque d and the result list result both have a maximum size of n. Therefore, the algorithm uses O(n) additional memory beyond the input list.

Method #4:  using list slicing and concatenation. 

Step-by-step approach:




lst = [5, 6, 8, 9, 10, 21, 3]
n = len(lst)
result = []
 
even = lst[::2]
odd = lst[1::2][::-1]
 
result = even + odd
 
print("Original list:", lst)
print("Alternate cyclic iteration:", result)

Output
Original list: [5, 6, 8, 9, 10, 21, 3]
Alternate cyclic iteration: [5, 8, 10, 3, 21, 9, 6]

Time complexity: O(n)
Auxiliary space: O(n) for the result list.


Article Tags :