Jailbone
Jailbone

Reputation: 93

Fastest way to do this concatenation of lists

I am looking for the most efficient way to concatenate lists in this fashion:

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

Desired result:

result = [[[1,2,3], [1,2,3]], [[4,5,6], [4,5,6]]]

I could not find an easy one-liner and am quite confused by all the different methods available.

Upvotes: 0

Views: 167

Answers (6)

Adon Bilivit
Adon Bilivit

Reputation: 27120

Surely it's as simple as:

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

c = [[a_,b_] for a_, b_ in zip(a, b)]

print(c)

Output:

[[[1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6]]]

Upvotes: 1

Jailbone
Jailbone

Reputation: 93

Thank you everybody! All methods work great but there is one thing i need that is missing. I should have been more precise, sorry. I also want the code to be flexible in such a way that I can continue to insert arrays in such a fashion that if the next incoming data is:

c = [[1,2,3], [4,5,6]]

I can continue inserting it so that the arrays grows like:

result = [[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6], [4, 5, 6]]]

This seems to be a little bit more complex I guess.

Upvotes: 1

Avinash
Avinash

Reputation: 875

You can do it using list comprehension. Try this:

a = [[1,2,3], [4,5,6]]
b = [[1,2,3], [4,5,6]]
result = [list(x) for x in zip(a,b)]

print(newlist)

Output:

[[[1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6]]]

Upvotes: 2

yannvm
yannvm

Reputation: 428

With numpy you can use the np.stack command.

import numpy as np

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

result = np.stack((a,b), axis=-2)

Output

array([[[1, 2, 3],
        [1, 2, 3]],

       [[4, 5, 6],
        [4, 5, 6]]])

And, if the list format is required, you can convert it to list using the .tolist() method.

np.stack((a,b), axis=-2).tolist()

Output

[[[1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6]]]

Upvotes: 2

Soof Golan
Soof Golan

Reputation: 1520

This works and should be quite fast as it is lazily evaluated:

from itertools import chain

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

result = chain.from_iterable(zip(a,b))
print(list(result))
# prints: [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]]
Side note

You could also use the More Itertools package which contains lots of performant recipes that augment the standard itertools module for handling iterators and collections.

In this scenario you could use more_itertools.interleave(a, b) to achive exactly the same effect.

Upvotes: 1

lemon
lemon

Reputation: 15492

I would do it using Python zip and map built-in functions this way:

list(map(list, zip(a,b)))

Output:

[[[1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6]]]

Explanation:

  • zip(a,b): pairs up each list between the two variables (outputs [([1, 2, 3], [1, 2, 3]), ([4, 5, 6], [4, 5, 6])])
  • map(list, zip(...)): converts every element of the outer list from tuple to list (outputs [[[1, 2, 3], [1, 2, 3]), ([4, 5, 6], [4, 5, 6]]])
  • list(...): used for printing purposes

Upvotes: 1

Related Questions