Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Append suffix/prefix to strings in a Python list?
In Python, you often need to add prefixes or suffixes to all strings in a list. This is common when formatting data, adding file extensions, or modifying text elements. Python provides several built-in methods like map(), list comprehensions, and reduce() to accomplish this efficiently.
Using map() Function
The map() function applies a given function to each item in a list. Combined with lambda expressions, it's perfect for adding prefixes and suffixes ?
# Original list of strings
items = ['note', 'book', 'pen']
suffix = 's'
prefix = 'my_'
# Adding suffix using map() and lambda
suffix_list = list(map(lambda x: x + suffix, items))
# Adding prefix using map() and lambda
prefix_list = list(map(lambda x: prefix + x, items))
print("Original list:", items)
print("With suffix:", suffix_list)
print("With prefix:", prefix_list)
Original list: ['note', 'book', 'pen'] With suffix: ['notes', 'books', 'pens'] With prefix: ['my_note', 'my_book', 'my_pen']
Using List Comprehension
List comprehension offers a more Pythonic and readable approach ?
items = ['note', 'book', 'pen']
suffix = 's'
prefix = 'my_'
# Adding suffix using list comprehension
suffix_list = [item + suffix for item in items]
# Adding prefix using list comprehension
prefix_list = [prefix + item for item in items]
print("With suffix:", suffix_list)
print("With prefix:", prefix_list)
With suffix: ['notes', 'books', 'pens'] With prefix: ['my_note', 'my_book', 'my_pen']
Using reduce() Function
The reduce() function from the functools module can also accomplish this task ?
from functools import reduce
items = ['note', 'book', 'pen']
suffix = 's'
prefix = 'my_'
# Adding suffix using reduce()
suffix_list = reduce(lambda acc, item: acc + [item + suffix], items, [])
# Adding prefix using reduce()
prefix_list = reduce(lambda acc, item: acc + [prefix + item], items, [])
print("With suffix:", suffix_list)
print("With prefix:", prefix_list)
With suffix: ['notes', 'books', 'pens'] With prefix: ['my_note', 'my_book', 'my_pen']
Adding Both Prefix and Suffix
You can combine both prefix and suffix operations in a single step ?
items = ['note', 'book', 'pen']
prefix = 'my_'
suffix = '.txt'
# Adding both prefix and suffix
result = [prefix + item + suffix for item in items]
print("With prefix and suffix:", result)
With prefix and suffix: ['my_note.txt', 'my_book.txt', 'my_pen.txt']
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List Comprehension | Excellent | Fast | Most scenarios |
map() |
Good | Fast | Functional programming style |
reduce() |
Complex | Slower | Academic purposes |
Conclusion
List comprehension is the most Pythonic way to add prefixes and suffixes to strings in a list. Use map() for functional programming approaches, and avoid reduce() for this task as it's unnecessarily complex.
