Add String to Each Element in List in Python (4 Examples)

 

In this Python tutorial, you’ll learn how to add a string to each element in a list of strings.

The article contains the following topics:

Do you want to know more? Check it out!

 

Create Example Data

The first step in this tutorial is to create some data to use as an example.

my_list = ["apple", "banana", "strawberry", "watermelon"]

As you can see, we have created a list object called my_list, which contains four strings. Let’s see how to concatenate a string to each one of the elements in my_list!

 

Example 1: Add String to Elements in List by List Comprehension & Concatenation

In this first example, we will use a list comprehension and the + operator to create a new list in which each element is added to the string fruit:. Take a look.

my_list = ["fruit: " + x for x in my_list]
print(my_list)
# ['fruit: apple', 'fruit: banana', 
# 'fruit: strawberry', 'fruit: watermelon']

The new my_list now contains the added string. Great!

 

Example 2: Add String to Elements in List Using for Loop & Concatenation

Using a for loop can also be a good choice to add a string to all elements in a list.

for i in range(len(my_list)):
    my_list[i] = "fruit: " + my_list[i]
print(my_list)
# ['fruit: apple', 'fruit: banana', 
# 'fruit: strawberry', 'fruit: watermelon']

In this approach, we loop through the indices of my_list. Then, the string fruit: is concatenated with each element using the + operator.

 

Example 3: Add String to Elements in List Using map() & lambda() Functions

In this example, we will use the map() and lambda() functions to concatenate a string to each of the elements in a list.

my_list = list(map(lambda x: "fruit: " + x, my_list))
print(my_list)
# ['fruit: apple', 'fruit: banana', 
# 'fruit: strawberry', 'fruit: watermelon']

As you can see in the previous Python output, the lambda() function takes an element as input and returns the concatenation of fruit: with the element. The map() function returns a map object that is converted to a list using the list() function.

 

Example 4: Add String to Elements in List by List Comprehension & f-strings

This last approach shows how to use list comprehension and f-strings to add a string to all the elements in our list.

my_list = [f"fruit: {x}" for x in my_list]
print(my_list)
# ['fruit: apple', 'fruit: banana', 
# 'fruit: strawberry', 'fruit: watermelon']

As shown, the f-string expression inside the square brackets is evaluated for each element of the original list, where the variable x is replaced by the current element. We obtained the same result as in the previous examples, just as expected 🙂

 

Video, Further Resources & Summary

Do you need more explanations on how to add a string to each element in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

In addition, you may want to have a look at the related programming tutorials on Statistics Globe:

This post has shown how to concatenate a string to each element in a list of strings in Python. In case you have further questions, please let me know in the comments section.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top