![]() |
VOOZH | about |
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.
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.
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
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
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
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
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:
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:
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.