Sometimes, while working with string lists, we can have a problem in which we need to pad each string in the list with a particular string. This type of problem can come in many places in the web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1: Using list comprehension This task can be performed using list comprehension. In this, we iterate each string element and reconstruct a new string list after adding the required string at the rear and front of each string.
Python3
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
res = [pad_str + ele + pad_str for ele in test_list]
print ( "The String list after padding : " + str (res))
|
Output
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) as a new list “res” is created which contains n elements.
Method #2: Using list comprehension + string formatting This task can also be performed using a combination of above functionalities. In this, we perform the task of padding using formatted string than + operator.
Python3
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
temp = pad_str + '{0}' + pad_str
res = [temp. format (ele) for ele in test_list]
print ( "The String list after padding : " + str (res))
|
Output
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), where n is the length of the result list after padding.
Method #3 : Using + and replace() method
Python3
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
x = pad_str + " " + pad_str
res = []
for i in test_list:
a = x.replace( " " , i)
res.append(a)
print ( "The String list after padding : " + str (res))
|
Output
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as a new list of the same length as the input list is created to store the padded strings.
Method #4 : Using map() and lambda function
Python3
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
res = list ( map ( lambda x: pad_str + x + pad_str, test_list))
print ( "The String list after padding : " + str (res))
|
Output
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using * and join() methods
- Created a list with the padding string as 2 elements of list(using *)
- Initiated a for loop to traverse the test_list
- Joined the padded string list with each element of for loop(using join())
- Appended the joined string to output list
Python3
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
x = [pad_str] * 2
res = []
for i in test_list:
res.append(i.join(x))
print ( "The String list after padding : " + str (res))
|
Output
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time Complexity : O(n)
Auxiliary Space : O(n)
Method #6: Using List Concatenation and String Multiplication
Python3
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
pad_list = [pad_str] * 2
res = []
for i in test_list:
res.append(pad_list[ 0 ] + i + pad_list[ 1 ])
print ( "The String list after padding : " + str (res))
|
Output
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time Complexity : O(n)
Auxiliary Space : O(n)
Method 7: Using the zip() function and string concatenation
In this method, we can use the zip() function to create a list of tuples, where each tuple contains an element from the original list and the padding string. We can then concatenate the elements of each tuple using string concatenation to create the padded strings.
Here are the steps:
- Initialize the original list and the padding string, just like in the previous methods.
- Use the zip() function to create a list of tuples, where each tuple contains an element from the original list and the padding string. We can use the itertools.cycle() function to repeat the padding string indefinitely.
- Iterate over the list of tuples and concatenate the elements of each tuple using string concatenation to create the padded strings.
- Append the padded strings to a new list.
- Print the new list.
Python3
import itertools
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
padded_list = []
for string, pad in zip (test_list, itertools.cycle([pad_str])):
padded_list.append(pad + string + pad)
print ( "The String list after padding : " + str (padded_list))
|
Output
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time complexity: O(n)
Auxiliary space: O(n)
Method 8: Using numpy:
Algorithm:
- Initialize the original list “test_list”.
- Initialize the pad string “pad_str”.
- Create an empty list “padded_list”.
- Iterate through each element in “test_list” using a for loop.
- For each element in “test_list”, concatenate the pad string and the element with the pad string again, and
- append the resulting string to “padded_list”.
- Print the resulting padded list.
Python3
import numpy as np
test_list = [ "a" , "b" , "c" ]
print ( "The original list : " + str (test_list))
pad_str = 'gfg'
arr = np.array(test_list)
padded_arr = np.char.add(np.char.add(pad_str, arr), pad_str)
padded_list = padded_arr.tolist()
print ( "The String list after padding : " + str (padded_list))
|
Output:
The original list : ['a', 'b', 'c']
The String list after padding : ['gfgagfg', 'gfgbgfg', 'gfgcgfg']
Time complexity:
The time complexity of the for loop that iterates through each element in “test_list” is O(n), where n is the length of the list.
The time complexity of the string concatenation is O(1).
Therefore, the overall time complexity of the code is O(n).
Space complexity:
The space complexity of the code is O(n), where n is the length of the list.
This is because the original list “test_list” and the resulting padded list “padded_list” both require O(n) space to store.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
08 May, 2023
Like Article
Save Article