This is a tips question for golfing in python.
Suppose you have two lists of strings, and you want to concatenate the corresponding entries from each list. E.g. with a=list("abcd")
and b=list("1234")
, calculate ["a1","b2","c3","d4"]
.
This is trivial in array-based programming languages, where operations generally apply memberwise to lists. For example, in my golfing language Pip, the code is simply a.b
. But in Python, it's not so easy.
The Pythonic way is probably to use zip
and a list comprehension (25 chars):
[x+y for x,y in zip(a,b)]
Another method is map
with a lambda function (23):
map(lambda x,y:x+y,a,b)
The following is the shortest I've come up with (21):
map("".join,zip(a,b))
Is there any shorter method?
Assume that the lists are the same length and that some kind of iterable is all that's needed (so a map
object is fine in Python 3).