-3

concatenate two elements as 1 element for example we are given two arrays a and b:

a=[1,2,3]
b=[4,5,6]

so what I want is an output as

14, 25, 36
dawg
  • 98,345
  • 23
  • 131
  • 206
Chandler
  • 15
  • 3

2 Answers2

3

If a and b are lists of integers, you could do the following:

c = [i * 10 + j for i, j in zip(a, b)]

If they are lists of strings, you could do the following:

c = [i + j for i, j in zip(a, b)]
Michael Hodel
  • 2,845
  • 1
  • 5
  • 10
0
result = [int(f'{x}{y}') for x, y in zip(a, b)]
MeBex
  • 488
  • 1
  • 5
  • 20
  • For the given input, this is considerably slower than doing the math. – chepner Apr 14 '22 at 20:08
  • Yeah, doing math is more optimized than this ;) – MeBex Apr 14 '22 at 20:12
  • For a slight improvement: `int(f'{x}{y}')` is likely faster that str concat. (ref: [Why are f-strings faster than str() to parse values?](https://stackoverflow.com/q/56587807/15497888)) – Henry Ecker Apr 14 '22 at 21:16