Mash 2 arrays together so that the returning array has alternating elements of the 2 arrays . Both arrays will always be the same length.

eg. [1,2,3] + ['a','b','c'] =  [1, 'a', 2, 'b', 3, 'c']

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Data structures

Starter code: def array_mash(a, b):
	

Solutions:
def array_mash(xs, ys):
	return [z for p in zip(xs, ys) for z in p]


def array_mash(a, b):
	lista = []
	c = len(a)
	for i in range(len(a)):
		lista.append(a[i])
		lista.append(b[i])
	return lista


from itertools import chain

def array_mash(a, b):
	return list(chain(*zip(a, b)))


def array_mash(a, b):
	return [i for z in zip(a, b) for i in z]


array_mash = lambda a, b: list(sum(zip(a, b), ()))


def array_mash(a, b):
	out_list = list()
	for (i, j) in zip(a, b):
		out_list.append(i)
		out_list.append(j)
	return out_list


def array_mash(a, b):
	return [x for xs in zip(a, b) for x in xs]


def array_mash(a, b):
	for (v, y) in zip(range(1, len(b) * 2, 2), b):
		a.insert(v, y)
	return a


array_mash = lambda *a, c=__import__('itertools').chain: list(c(*zip(*a)))


def array_mash(a, b):
	r = [None] * (len(a) + len(b))
	r[::2] = a
	r[1::2] = b
	return r