0

I am new to python and I need to do this:

lines = ['apple','bear']
signed=['aelpp','aber']

I want the output to be:

res = ['aelpp apple', 'aber bear']

I'd appreciate if you can help! I've tried simply using + and the join() function but not getting what I want.

3 Answers 3

5

You can try using zip() and join():

res = [" ".join(e) for e in zip(signed, lines)]
print res

Output:

['aelpp apple', 'aber bear']

Edit: As @ThiefMaster commented, this can be made more compact using map():

res = map(' '.join, zip(signed, lines))
Sign up to request clarification or add additional context in comments.

1 Comment

Could be done in an even more compact way: map(' '.join, zip(signed, lines))
0

You can use map and zip:

list(map(lambda x: x[1] + ' ' + x[0], zip(lines, signed)))

1 Comment

map(lambda ... is always better written as a list comprehension/generator expression.
0

Since you are new to python, you may find the following easier to understand compared to the others:

>>> res = []
>>> for i in range(len(signed)):
...     res.append(signed[i] + ' ' + lines[i])

The results:

>>> print res
['aelpp apple', 'aber bear']

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.