Python | Lead and Trail padding of strings list
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
# Python3 code to demonstrate working of # Trail and lead padding of strings list # using list comprehension # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # Trail and lead padding of strings list # using list comprehension res = [pad_str + ele + pad_str for ele in test_list] # printing result print ( "The String list after padding : " + str (res)) |
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
# Python3 code to demonstrate working of # Trail and lead padding of strings list # using list comprehension + string formatting # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # Trail and lead padding of strings list # using list comprehension + string formatting temp = pad_str + '{0}' + pad_str res = [temp. format (ele) for ele in test_list] # printing result print ( "The String list after padding : " + str (res)) |
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
# Python3 code to demonstrate working of # Trail and lead padding of strings list # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # Trail and lead padding of strings list x = pad_str + " " + pad_str res = [] for i in test_list: a = x.replace( " " , i) res.append(a) # printing result print ( "The String list after padding : " + str (res)) |
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
# Python3 code to demonstrate working of # Trail and lead padding of strings list # using map() and lambda function # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # Trail and lead padding of strings list res = list ( map ( lambda x: pad_str + x + pad_str, test_list)) # printing result print ( "The String list after padding : " + str (res)) #this code is contributed by edula vinay kumar reddy |
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
# Python3 code to demonstrate working of # Trail and lead padding of strings list # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # Trail and lead padding of strings list x = [pad_str] * 2 res = [] for i in test_list: res.append(i.join(x)) # printing result print ( "The String list after padding : " + str (res)) |
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
# Python3 code to demonstrate working of # Trail and lead padding of strings list # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # Trail and lead padding of strings list pad_list = [pad_str] * 2 res = [] for i in test_list: res.append(pad_list[ 0 ] + i + pad_list[ 1 ]) # printing result print ( "The String list after padding : " + str (res)) |
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 # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # create list of padded strings using zip() and string concatenation padded_list = [] for string, pad in zip (test_list, itertools.cycle([pad_str])): padded_list.append(pad + string + pad) # printing result print ( "The String list after padding : " + str (padded_list)) |
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 # initialize list test_list = [ "a" , "b" , "c" ] # printing original list print ( "The original list : " + str (test_list)) # initialize pad_str pad_str = 'gfg' # create numpy array from test_list arr = np.array(test_list) # concatenate pad_str to the beginning and end of each element of arr padded_arr = np.char.add(np.char.add(pad_str, arr), pad_str) # convert padded_arr back to a list padded_list = padded_arr.tolist() # printing result print ( "The String list after padding : " + str (padded_list)) #This code is contributed by Vinay Pinjala |
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.