Reputation: 21
Turns out to be an extension, to do this. For example: "hello world" becomes "hweolrllod", where it is the 1st letter from hello, the 1st from world and so on.
Upvotes: 2
Views: 226
Reputation: 215107
If the two words are of same length, you can use zip
:
''.join(x for p in zip(*"hello world".split(" ")) for x in p)
# 'hweolrllod'
''.join(x for p in zip("hello", "world") for x in p)
# 'hweolrllod'
If they are not of the same length, and you want to keep the longer version, use zip_longest:
from itertools import zip_longest
''.join(x for p in zip_longest(*"he world".split(" "), fillvalue='') for x in p)
# 'hweorld'
''.join(x for p in zip_longest("he", "world", fillvalue='') for x in p)
# 'hweorld'
Upvotes: 6