3

I tried many times but could not do it.

Here is an example:

print( concat_corresponding( "12345", "6789XYZ" ) )

Desired output:

162738495XYZ
jpp
  • 159,742
  • 34
  • 281
  • 339
  • show us what you've tried...show us `concat_corresponding` code. Is `162738495XYZ` the expected output or what you're getting? – depperm Apr 27 '18 at 19:10
  • Did an answer below help? Feel free to accept an answer (green tick on left), or ask for clarification. – jpp May 08 '18 at 11:14

6 Answers6

3

This is one way with itertools:

from itertools import chain, zip_longest

a = "12345"
b = "6789XYZ"

res = ''.join(list(chain.from_iterable(zip_longest(a, b, fillvalue=''))))

# '162738495XYZ'

Note the list conversion is not required, but improves performance.

jpp
  • 159,742
  • 34
  • 281
  • 339
2

You can levarege list comprehensions, join and zip:

te1 = "12345"
te2 = "6789XYZ"

print (''.join( ''.join(x) for x in zip(te1,te2)) + (te1[len(te2):] if len(te1)>len(te2) else te2[len(te1):]))
                                                    # ^^^^ this part adds the remainer of longer list to result

output:

162738495XYZ

https://docs.python.org/3/library/functions.html#zip

https://docs.python.org/3/library/stdtypes.html#str.join

Explanation:

zip pairs up the characters by position, the rest makes the list of pairs back into a string.

Zip only works for the shorter length string - you can switch to itertools.zip_longest (see JimDennis answer) or append the longer lists part (like I did here)

itertools.zip_longest(*iterables, fillvalue=None)

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
2
from itertools import izip_longest
''.join(['%s%s' % (x ,y)\
         for x,y in izip_longest("12345","6789XYZ", fillvalue='')])

## Was: ''.join(['%s%s' % (x if x else '',y if y else '') \
##         for x,y in izip_longest("12345","6789XYZ")])

To break that down a bit:

  • the builtin zip() function zips only to the shorter of the two sequences; so we use izip_longest() from the itertools standard library module
  • izip_longest() pads the generated sequences with None by default; so we add the fillvalue='' optional (keyword) argument
  • the resulting substrings are all just joined together to form your results.
Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
  • 1
    You can simply give `''` as fillvalue: [itertools.zip_longest(*iterables, fillvalue=None)](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) no need to munge – Patrick Artner Apr 27 '18 at 19:24
1

I think this solution is a little cleaner and takes advantage of the fillvalue keyword argument for itertools.zip_longest.

from itertools import zip_longest 

''.join(a+b for a, b in zip_longest(s1, s2, fillvalue=''))
bphi
  • 3,115
  • 3
  • 23
  • 36
0

This one is basic but useful

str1="12345"
str2="6789XYZ"
str3=""
i=0
for i, ch in enumerate(zip(str1, str2)):
    str3 += ch[0] + ch[1]

if len(str1) < len(str2):
    str3 += str2[i+1:]
else:
    str3 += str1[i+1:]
print(str3)
A.Raouf
  • 2,171
  • 1
  • 24
  • 36
0

Another way of doing it is the following:

def concat_corresponding(string1, string2):
    minimum, maximum = sorted([string1, string2], key=len)
    return "".join(string1[i]+string2[i] for i in range(len(minimum))) + maximum[len(minimum):]

print(concat_corresponding( "12345", "6789XYZ" )) 

Output:

162738495XYZ   
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29