Categories
Python Answers

How to append the same string to a list of strings in Python?

Spread the love

Sometimes, we want to append the same string to a list of strings in Python.

In this article, we’ll look at how to append the same string to a list of strings in Python.

How to append the same string to a list of strings in Python?

To append the same string to a list of strings in Python, we can use list comprehension.

For instance, we write:

list1 = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
list2 = [s + string for s in list1]
print(list2)

We concatenate string to each entry s in list1.

And then we assign the returned list to list2.

Therefore, list2 is ['foobar', 'fobbar', 'fazbar', 'funkbar'].

Conclusion

To append the same string to a list of strings in Python, we can use list comprehension.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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