0

so i saw this as an answer to a question from a year ago about adding/extending a string, etc.

s  = 'foo'
s += 'bar'
s += 'baz'

l = []
l.append('foo')
l.append('bar')
l.append('baz')

my question is how would one combine these two features? l would return:

['foo','bar','baz']

but what if i wanted to add a letter to the end of each string in the list and then have it return:

['food','bars','bazy']

is this a thing or is it more wishful thinking?

1
  • If you want to add a different character to the end of the elements of the array, then you will have to iterate over each of them, and modify them in-place. Commented Oct 8, 2012 at 20:41

2 Answers 2

5

I'm not sure I understand. Are you looking for something like this:

first_list = ['foo','bar','baz']
second_list = [x+y for x,y in zip(first_list,'dsy')]
Sign up to request clarification or add additional context in comments.

Comments

1

you can use zip() along with join() and map().

In [72]: lis=['foo','bar','baz']

In [73]: map("".join,zip(lis,'dsy')) 
Out[73]: ['food', 'bars', 'bazy']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.