tags:

views:

155

answers:

5

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:

list = ['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.

+12  A: 

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.

gahooa
Or a genexp if you want it lazily `(s + mystring for s in mylist)`
Noufal Ibrahim
That definitely did the trick, thank very much, still wrapping my head around list comprehension, if you know a good tutorial on it. before each item in the list, there is a u', is that for unicode?
Kevin
@Kevin, yes, `u''` indicates a Unicode string.
Peter Hansen
@Kevin, here's a tutorial for unicode strings, http://docs.python.org/tutorial/introduction.html#tut-unicodestrings
tgray
A: 
list2 = ['%sbar' % (x,) for x in list]

And don't use list as a name; it shadows the built-in type.

Ignacio Vazquez-Abrams
Why `'%sbar' % (x,)` instead of `'%sbar' % x`? Why not `x + 'bar'`?
John Machin
The second will fail if x happens to be a tuple. Obviously you *plan* to have every element be a string, but sometimes things go wrong.The difference between the first and the third is mostly taste, unless you get the string from an external source.
Ignacio Vazquez-Abrams
'raise exception' != 'fail'. If you have the wrong data type, you have *already* failed. My preferred expression raises an exception highlighting the failure; your preferred expression silently produces garbage. Taste: baroque slow expressions are not to my taste.
John Machin
A: 
new_list = [word_in_list + end_string for word_in_list in old_list]

Using names such as "list" for your variable names is bad since it will overwrite/override the builtins.

Casey
A: 
my_list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
my_new_list = [x + string for x in my_list]
print my_new_list

This will print:

['foobar', 'fobbar', 'fazbar', 'funkbar']
Tendayi Mawushe
A: 

I havent found a way to comment on answers till now. So here it is. I support Ignacio Vazquez-Abrams's answer of list2 = ['%sbar' % x for x in list].

Others answers with [string + "bar" for string in list] would work for most of times, but if you accept a more general solution for the simplest case you're - IMHO - following Python Design Principles. There should be preferably one obvious way to do it. %sbar works all the time.

jeffjose
StackOverflow restricts commenting on other posts until you're at 50 points
Wallacoloo
@jeffjose: Ignacio's answer was in fact `list2 = ['%sbar' % (x,) for x in list]`. Please give examples where x refers to a string and `'%sbar' % x` "works" and `x + 'bar'` doesn't.
John Machin
The original question specified two strings. `s1+s2` will always work (and most efficiently) in that case.
gahooa
@John Machin, Yes, what Ignacio said is correct and I merely added onto his post with my answer. And for your question as to why `%sbar` works all the time and `x + "bar"` in some cases .. True if `x` is a string there's absolutely no doubt, both answers are correct. But as soon as you get out `strings` you have to _learn_ a new answer for concatenation. To me all concatenation goes like `%sbar` whether it is `string` or `integer`. I feel that would make the whole code more consistent.
jeffjose
@jeffjose: (1) Correctness wasn't the issue. You said Ignacio used `'%sbar' % x`; he didn't; he used `'%sbar' % (x,)` -- re-read his answer. (2) You concatenate integers using `'%sbar'`, do you? That's a strange notion of concatenation. Example, please -- I thought that `string % expression` was for formatting, not concatenation. To concatenate lists (consistent with concatenating strings) I use `+` e.g. `a=[1,2];b=[3,4];c=a+b;print c` -- how do you concatenate lists using `"%sbar"`?
John Machin