Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Incremental and Cyclic Repetition of List Elements

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 + enumerate() 
This is brute force way in which this task can be performed. In this, we iterate the elements and perform the required number of repetition using mod operator and multiplication logic.

Python3




# Python3 code to demonstrate
# Incremental and Cyclic Repetition of List Elements
# using loop + enumerate()
 
# Initializing list
test_list = ['g', 'f', 'g', 'C', 'S']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing range
i, j = 2, 4
 
# Incremental and Cyclic Repetition of List Elements
# using loop + enumerate()
res = []
temp = list(range(i, j + 1))
for idx, ele in enumerate(test_list):
    res.append(ele * temp[idx % len(temp)])
     
# printing result
print ("Repetition List is : " + str(res))


Output : 

The original list is : ['g', 'f', 'g', 'C', 'S']
Repetition List is : ['gg', 'fff', 'gggg', 'CC', 'SSS']

 

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as we are using a new list to store the repeated elements.

Method #2 : Using cycle() + loop + zip() 
The combination of these tasks can also be used to perform this task. In this, we iterate using loop and cycling and repetition is performed using cycle().
 

Python3




# Python3 code to demonstrate
# Incremental and Cyclic Repetition of List Elements
# using cycle() + loop + zip()
from itertools import cycle
 
# Initializing list
test_list = ['g', 'f', 'g', 'C', 'S']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing range
i, j = 2, 4
 
# Incremental and Cyclic Repetition of List Elements
# using cycle() + loop + zip()
res = []
for k, l in zip(cycle(range(i, j + 1)), test_list):
    res.append(k * l)
     
# printing result
print ("Repetition List is : " + str(res))


Output : 

The original list is : ['g', 'f', 'g', 'C', 'S']
Repetition List is : ['gg', 'fff', 'gggg', 'CC', 'SSS']

 

Time complexity: O(n), where n is the length of the input list test_list. 
Auxiliary space: O(n), where n is the length of the input list test_list.

Method 3: Use list comprehension

Step-by-step approach:

  • Initialize the list test_list.
  • Print the original list.
  • Initialize the variables i and j.
  • Use list comprehension to create a new list res.
  • The enumerate() function is used to iterate over each element of test_list along with its index.
  • Use the modulus operator to cycle through the range of values from i to j, and multiply the value at the current index with the current value from the cycle.
  • Print the final result.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# Incremental and Cyclic Repetition of List Elements
# using list comprehension
 
# Initializing list
test_list = ['g', 'f', 'g', 'C', 'S']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing range
i, j = 2, 4
 
# Incremental and Cyclic Repetition of List Elements
# using list comprehension
res = [((idx % (j - i + 1) + i) * val) for idx, val in enumerate(test_list)]
 
# printing result
print("Repetition List is : " + str(res))


Output

The original list is : ['g', 'f', 'g', 'C', 'S']
Repetition List is : ['gg', 'fff', 'gggg', 'CC', 'SSS']

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list. This is the space required to store the output list res.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments