VOOZH about

URL: https://www.geeksforgeeks.org/python/python-append-suffix-prefix-to-strings-in-list/

⇱ Python | Append suffix/prefix to strings in list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Append suffix/prefix to strings in list

Last Updated : 3 May, 2023

Sometimes, while working with Python, we can a problem in which we need to pad strings in lists at trailing or leading position. This kind of problem is quite common and can occur in day-day programming or web development. Let's discuss a way in which this task can be performed. 

Method #1: Using + operator + list comprehension In this task, we just append the string at rear or front position using + operator and list comprehension is used to iterate through all the elements. 

Output : 
 
The original list : ['a', 'b', 'c', 'd']
list after prefix addition : ['gfga', 'gfgb', 'gfgc', 'gfgd']
list after suffix addition : ['agfg', 'bgfg', 'cgfg', 'dgfg']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using itertools.starmap(): The python library itertools provides a function called "starmap()" which can be used to apply the same function to multiple inputs from an iterable, in this case, it can be used to append the suffix/prefix to each string in the list, this approach would have a time complexity of O(n) and auxiliary space of O(n) as well.


Output
The original list : ['a', 'b', 'c', 'd']
list after prefix addition : ['gfga', 'gfgb', 'gfgc', 'gfgd']
list after suffix addition : ['agfg', 'bgfg', 'cgfg', 'dgfg']

Method #3: Using map and lambda functions


Output
The original list : ['a', 'b', 'c', 'd']
list after prefix addition : ['gfga', 'gfgb', 'gfgc', 'gfgd']
list after suffix addition : ['agfg', 'bgfg', 'cgfg', 'dgfg']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using For loop.


Output
The original list : ['a', 'b', 'c', 'd']
list after prefix addition : ['gfga', 'gfgb', 'gfgc', 'gfgd']
list after suffix addition : ['agfg', 'bgfg', 'cgfg', 'dgfg']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using numpy module:

Step-by-step approach:

  • Initialize an empty list to store the result of prefix addition and another empty list to store the result of suffix addition.
  • Iterate through the original list, and for each element, append the prefix to the beginning of the element and append the result to the prefix addition list.
  • Similarly, append the suffix to the end of the element and append the result to the suffix addition list.
  • Print the original list, prefix addition list, and suffix addition list.

Output:

The original list : ['a', 'b', 'c', 'd']
list after prefix addition : ['gfga' 'gfgb' 'gfgc' 'gfgd']
list after suffix addition : ['agfg' 'bgfg' 'cgfg' 'dgfg']

Time complexity: O(n), where n is the number of elements in the input list. This is because we iterate through each element once to perform the prefix and suffix addition.
Auxiliary Space: O(n), where n is the number of elements in the input list. This is because we create two lists of size n to store the results of the prefix and suffix addition

Method #6: Using the reduce() function from the functools module

Step-by-step approach:

  • Import the reduce() function from the functools module.
  • Create a test_list of strings.
  • Create two variables prefix and suffix with the desired string values.
  • Use the reduce() function with a lambda expression to iterate through each element item in test_list, concatenate prefix to it, and append the result to pre_res.
  • Use the reduce() function with a lambda expression to iterate through each element item in test_list, concatenate suffix to it, and append the result to suf_res.
  • Print the original list, test_list, the list after prefix addition, pre_res, and the list after suffix addition, suf_res.

Output
The original list : ['a', 'b', 'c', 'd']
List after prefix addition : ['gfga', 'gfgb', 'gfgc', 'gfgd']
List after suffix addition : ['agfg', 'bgfg', 'cgfg', 'dgfg']

Time complexity: O(n), where n is the length of test_list. 
Auxiliary space: O(n), where n is the length of test_list.

Comment