Appending the same string to a list of strings in Python

list, python

Solution

The simplest way to do this is with a list comprehension:

[s + mystring for s in mylist]

Notice that I avoided using builtin names like `list` because that shadows or hides the builtin names, which is very much not good.

Also, if you do not actually need a list, but just need an iterator, a generator expression can be more efficient (although it does not likely matter on short lists):

(s + mystring for s in mylist)

These are very powerful, flexible, and concise. Every good python programmer should learn to wield them.

Problem

I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example: ``` list1 = ['foo', 'fob', 'faz', 'funk'] string = 'bar' *magic* list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar'] ``` I tried for loops, and an attempt at list comprehension, but it was garbage. As always, any help, much appreciated.

Original source