Add a character to each item in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Add a character to each item in a List in Python #

To add a character to each item in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the addition (+) operator to add a character to each list item.
  3. The new list will contain the updated values.
main.py
my_list = ['bobby', 'hadz', 'com'] # ✅ Add a character to the END of each item in a list new_list = [item + '_' for item in my_list] print(new_list) # 👉️ ['bobby_', 'hadz_', 'com_'] # ------------------------------------------------------ # ✅ Add a character to the BEGINNING of each item in a list new_list = ['_' + item for item in my_list] print(new_list) # 👉️ ['_bobby', '_hadz', '_com']

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 add a character to each list item.

main.py
my_list = ['bobby', 'hadz', 'com'] new_list = [item + '_' for item in my_list] print(new_list) # 👉️ ['bobby_', 'hadz_', 'com_']

It doesn't have to be a single character, you can use the addition (+) operator to add any string to the list items.

The example above adds a character to the end of each list item.

However, the same approach can be used to add a character to the beginning of each list item.

main.py
my_list = ['bobby', 'hadz', 'com'] new_list = ['===' + item for item in my_list] print(new_list) # 👉️ ['===bobby', '===hadz', '===com']

Make sure the values on the left-hand and right-hand sides of the addition (+) operator are strings.

If you have a list of integers, use the str() class to convert each integer to a string before using the addition operator.
main.py
my_list = [1, 2, 3] new_list = [str(item) + '_' for item in my_list] print(new_list) # 👉️ ['1_', '2_', '3_']

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.

Add a character to each item in a List using a for loop #

To add a character to each item in a list:

  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 add a character to each list item.
main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): my_list[index] = item + '===' print(my_list) # 👉️ ['bobby===', 'hadz===', 'com===']

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 add a string to the value of the current list item.

You can use the same approach if you need to add a character to the beginning of each list item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): my_list[index] = '_' + item print(my_list) # 👉️ ['_bobby', '_hadz', '_com']

When using this approach, we mutate the original list.

Alternatively, you can use the map() function.

Add a character to each item in a List using map() #

To add a character to each item in a list:

  1. Call the map() function with a lambda function and the list.
  2. The lambda function should use the addition (+) operator to add a character 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 + '_', my_list)) print(new_list) # 👉️ ['bobby_', 'hadz_', 'com_']

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, adds a character to each string 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.