Append the same string to a List of strings in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Append the same string to a List of strings in Python #

To append the same string to a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Use the addition (+) operator to append the string to each list item.
  3. The new list will contain the updated values.
main.py
my_list = ['bobby', 'hadz', 'com'] new_list = [item + '_XYZ' for item in my_list] print(new_list) # 👉️ ['bobby_XYZ', 'hadz_XYZ', 'com_XYZ']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the addition (+) operator to append a string to the current list item and return the result.

If the list contains values of any other type, you have to use the str() class to convert them to strings before using the addition operator.

main.py
my_list = [2, 4, 6] new_list = [str(item) + '_XYZ' for item in my_list] print(new_list) # 👉️ ['2_XYZ', '4_XYZ', '6_XYZ']

The values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

Alternatively, you can use a simple for loop.

Append the same string to a List of strings using a for loop #

To append the same string to a list of strings:

  1. Use the enumerate() function to get access to the index of the current iteration.
  2. Use a for loop to iterate over the enumerate object.
  3. Use the addition (+) operator to append the string to each list item.
main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): my_list[index] = item + '_XYZ' print(my_list) # 👉️ ['bobby_XYZ', 'hadz_XYZ', 'com_XYZ']

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 bobby, 1 hadz, 2 com

On each iteration, we use the addition (+) operator to append a string to the value of the current list item.

When using this approach, we mutate the original list.

The list comprehension approach creates a new list without mutating the original list.

Alternatively, you can use the map() function.

Append the same string to a List of strings using map() #

To append the same string to a list of strings:

  1. Call the map() function with a lambda function and the list.
  2. The lambda function should use the addition (+) operator to append a string to each list item.
  3. Use the list() class to convert the map object to a list.
main.py
my_list = ['bobby', 'hadz', 'com'] new_list = list(map(lambda item: item + '_XYZ', my_list)) print(new_list) # 👉️ ['bobby_XYZ', 'hadz_XYZ', 'com_XYZ']

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The lambda function gets called with each item in the list, appends a string to each value and returns the result.

The last step is to use the list() class to convert the map object to a list.

Which approach you pick is a matter of personal preference. I'd go with using a list comprehension as I find them quite intuitive and easy to read.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.