Open In App

Python – String Repetition and spacing in List

Sometimes while working with Python, we can have a problem in which we need to perform the repetition of each string in list and also attach a deliminator to each occurrence. This kind of problem can occur in day-day programming. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop 
This task can be performed in brute force way using loop. In this, we iterate the list and perform string addition and multiplication while iteration using suitable operators.






# Python3 code to demonstrate working of
# String Repetition and spacing in List
# Using loop
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = '-'
 
# initializing K
K = 3
 
# String Repetition and spacing in List
# Using loop
res = []
for sub in test_list:
    res.append((sub + delim) * K)
     
# printing result
print("List after performing operations : " + str(res))

Output : 
The original list is : ['gfg', 'is', 'best']
List after performing operations : ['gfg-gfg-gfg-', 'is-is-is-', 'best-best-best-']

 

Time complexity: O(n), where n is the length of the input list, because we loop through each element of the list once.
Auxiliary space: O(n), because we create a new list of length n to store the results.



 
Method #2 : Using join() + list comprehension 
The combination of above functionalities can also be used to perform this task. In this, we perform the task of attaching delim using join() and list comprehension performs the task of repetition. Avoids trailing delim.




# Python3 code to demonstrate working of
# String Repetition and spacing in List
# Using join() + list comprehension
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = '-'
 
# initializing K
K = 3
 
# String Repetition and spacing in List
# Using join() + list comprehension
res = []
for sub in test_list:
    res.append(delim.join([sub for _ in range(K)]))
     
# printing result
print("List after performing operations : " + str(res))

Output : 
The original list is : ['gfg', 'is', 'best']
List after performing operations : ['gfg-gfg-gfg', 'is-is-is', 'best-best-best']

 

Time complexity: O(n*K), where n is the length of the input list and K is the repetition factor.
Auxiliary space: O(n*K), as we are creating a new list with K repetitions of each element in the input list.

Method #3: Using itertools.repeat() 

Here’s an example of using itertools.repeat() to repeat each string in the list test_list and attach a delimiter to each occurrence:

Step-by-step approach:

Below is the implementation of the above approach:




import itertools
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = '-'
 
# initializing K
K = 3
 
# String Repetition and spacing in List
# Using itertools.repeat()
res = []
for sub in test_list:
    res.append(delim.join(list(itertools.repeat(sub, K))))
 
# printing result
print("List after performing operations : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy

Output
The original list is : ['gfg', 'is', 'best']
List after performing operations : ['gfg-gfg-gfg', 'is-is-is', 'best-best-best']

Time complexity: O(n * K) where n is the length of the list test_list and K is the number of repetitions.
Auxiliary Space: O(n * K) as we are creating a list of length n * K to store the results.

Method 4: Using map() and lambda functions.

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# String Repetition and spacing in List
# Using map() and lambda functions
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = '-'
 
# initializing K
K = 3
 
# String Repetition and spacing in List
# Using map() and lambda functions
res = list(map(lambda x: delim.join([x]*K), test_list))
 
# printing result
print("List after performing operations : " + str(res))

Output
The original list is : ['gfg', 'is', 'best']
List after performing operations : ['gfg-gfg-gfg', 'is-is-is', 'best-best-best']

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n) as we are storing the output in a list.

Method 5: Using numpy.tile() and numpy.ravel()

Step-by-step approach:

  1. Import the numpy module.
  2. Initialize the list test_list.
  3. Print the original list.
  4. Initialize the delimiter delim and the number of repetitions K.
  5. Use a list comprehension to create a new list res with the repeated and spaced strings. The expression inside the brackets uses numpy.tile() to repeat each string in test_list K times vertically and 1 time horizontally, and numpy.ravel() to flatten the resulting 2D array into a 1D array. The join() method is then used to concatenate the resulting array of strings with the delimiter delim.
  6. Print the resulting list.




import numpy as np
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = '-'
 
# initializing K
K = 3
 
# String Repetition and spacing in List
# Using numpy.tile() and numpy.ravel()
res = [delim.join(np.ravel(np.tile([x], (K, 1)))) for x in test_list]
 
# printing result
print("List after performing operations : " + str(res))

OUTPUT:

The original list is : ['gfg', 'is', 'best']
List after performing operations : ['gfg-gfg-gfg', 'is-is-is', 'best-best-best']

Time complexity: The time complexity of this method is O(nk), where n is the length of the input list test_list and k is the number of repetitions K, since the operation of repeating each string in test_list K times has a time complexity of O(k|s|), where |s| is the length of the string.

Auxiliary space: The auxiliary space used by this method is O(nk), since a new list of size n*k is created.

Method 6: Using list multiplication and string concatenation

Step-by-step approach:

Below is the implementation of the above approach:




# Method 6: Using list multiplication and string concatenation
 
# initializing list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delim
delim = '-'
 
# initializing K
K = 3
 
# String Repetition and spacing in List
# Using list multiplication and string concatenation
res = [delim.join([s] * K) for s in test_list]
 
# printing result
print("List after performing operations : " + str(res))

Output
The original list is : ['gfg', 'is', 'best']
List after performing operations : ['gfg-gfg-gfg', 'is-is-is', 'best-best-best']

 Time complexity: O(N * K), where N is the length of test_list.
Auxiliary space: O(N * K), where N is the length of test_list. This is because a new list of length N * K is created to store the result.


Article Tags :