VOOZH about

URL: https://www.geeksforgeeks.org/python/python-lead-and-trail-padding-of-strings-list/

⇱ Python | Lead and Trail padding of strings list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Lead and Trail padding of strings list

Last Updated : 8 May, 2023

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. 


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. 


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


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


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

  1. Created a list with the padding string as 2 elements of list(using *)
  2. Initiated a for loop to traverse the test_list
  3. Joined the padded string list with each element of for loop(using join())
  4. Appended the joined string to output 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 #6: Using List Concatenation and String Multiplication


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.

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:

  1. Initialize the original list "test_list".
  2. Initialize the pad string "pad_str".
  3. Create an empty list "padded_list".
  4. Iterate through each element in "test_list" using a for loop.
  5. For each element in "test_list", concatenate the pad string and the element with the pad string again, and
  6. append the resulting string to "padded_list".
  7. Print the resulting 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.

Comment