How to Append suffix/prefix to strings in a Python list?


Introduction

In this article, the users will understand how to append suffix/prefix to strings in a Python list. A List would store elements of different data types within these “[]” brackets. The string manipulation would be possible in the Python language. Various inbuilt string method are available in Python to append suffix/prefix to strings in a Python list like reduce(), map(), and so on. The two approaches are demonstrated in the article to append the specified character either in the starting or the rear side to the string.

The methods to do this can be varying and some may be simple loop iteration on the other side it can be some of the functions and methods.

Syntax

reduce()

The collection module in python has many subclasses like “defaultdict()” and reduce() method. The reduce() method always uses two arguments and then reduces them to a single value.

map()  

It is a built-in Python function to iterate through every item of the list of numbers.

Approaches

Approach 1 − Using typing module

Approach 2 − Using functools module

Approach 1: Python program to append suffix/prefix to strings using typing module

Two lists are defined with three elements in one list and the element to be added to the first list is also declared as a string. Then the function is called to get the values of the list and it is printed.

Algorithm

  • Step 1 − Importing the required function from typing module

  • Step 2 − The function with three arguments is defined to add suffix or prefix to a list

  • Step 3 − Adding the suffix/prefix to each element of the list using map() and lambda expression

  • Step 4 − The complete list after the append() function is printed.

Example

# Importing the list function from typing module
from typing import List

# Defining the function to add suffix/prefix to a list
def add_suffix_prefix_to_list(lst: List[str], suffix: str, prefix: str) -> List[str]:
   # Adding the suffix to list’s element using map() and lambda expression
   suffix_list = list(map(lambda a: a + suffix, lst))

   # Adding the prefix to list’s element using map() and lambda expression
   prefix_list = list(map(lambda a: prefix + a, lst))

   # Returns the complete list after appending
   return suffix_list, prefix_list

# Initializing list with three elements
list_1 = ['note', 'book','pen']

# The additional elements that need to be added to the list
list_2 = 's'

# Calling the function to add suffix/prefix to the list
suffix_list, prefix_list = add_suffix_prefix_to_list(list_1, list_2, list_2)

# Printing the complete lists after adding suffix/prefix
print("suffix addition of element to list", suffix_list)
print("prefix addition of element to list", prefix_list)

Output

suffix addition of element to list ['notes', 'books', 'pens']
prefix addition of element to list ['snote', 'sbook', 'spen']

Approach 2: Python program to append suffix/prefix to strings using functools module

The functools library is imported and the two lists are defined with three elements in one list and the element to be added to the first list is also declared as a string. Then the reduce function is used to add each element of the list to the prefix and suffix respectively.

Algorithm

  • Step 1 − Importing the required function from functools module

  • Step 2 − The list is initialized with a list of elements of strings.

  • Step 3 − Another list is initialized to add a suffix or prefix to the existing list.

  • Step 4 − Using the reduce() function the appending is done and printed.

Example

# The reduce function is imported
from functools import reduce

# Initializing list with three elements
list_1 = ['note', 'book','pen']

# The additional elements that need to be added to the list
list_2 = 's'

# Adding the suffix to each element of the list using reduce() function
suffix_list = reduce(lambda x, y: x + [y + list_2], list_1, [])

# Adding the prefix to each element of the list using reduce() function
prefix_list = reduce(lambda x, y: x + [list_2 + y], list_1, [])

# Printing the complete lists after adding suffix/prefix
print("suffix addition of element to list", suffix_list)
print("prefix addition of element to list", prefix_list)

Output

suffix addition of element to list ['notes', 'books', 'pens']
prefix addition of element to list ['snote', 'sbook', 'spen']

Conclusion

The two approaches are illustrated in this article. The first approach imports the map() and list() functions from the typing library. The function is defined to add the prefix and suffix to the list. Later the map() function and lambda is used to add each element of the list to the prefix and suffix respectively.

Updated on: 25-Aug-2023

791 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements