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.