Reputation: 6762
I have three lists with elements :
a = [[0,1],[2,3],...]
b = [[5,6],[7,8],...]
c = []
I want to append elements from a and b into c to get:
c = [ [0,1],[5,6],[2,3],[7,8],.... ]
Upvotes: 2
Views: 1499
Reputation: 133514
Using more_itertools
which implements the itertools
roundrobin
recipe
>>> from more_itertools import roundrobin
>>> a = [[0,1],[2,3]]
>>> b = [[5,6],[7,8]]
>>> list(roundrobin(a, b))
[[0, 1], [5, 6], [2, 3], [7, 8]]
Upvotes: 1
Reputation: 55469
Assuming the two lists are the same length, the most compact way to do this uses itertools.chain
and zip
.
from itertools import chain
a = [[0,1],[2,3],[10,11],[12,13]]
b = [[5,6],[7,8],[15,16],[17,18]]
c = [*chain(*zip(a, b))]
print(c)
output
[[0, 1], [5, 6], [2, 3], [7, 8], [10, 11], [15, 16], [12, 13], [17, 18]]
As juanpa.arrivillaga mentions in the comments, that syntax will not work on older versions of Python. Instead, you can do
c = list(chain(*zip(a, b)))
Here's another option, which doesn't use imports or the *
splat operator:
c = [u for t in zip(a, b) for u in t]
If you need to handle input lists of unequal length, take a look at the roundrobin
function in Itertools Recipes. Eg,
c = list(roundrobin(a, b))
Upvotes: 2
Reputation: 48067
Another very simple approach using string slicing (and most performance efficient) as:
>>> a = [[0,1],[2,3]]
>>> b = [[5,6],[7,8]]
>>> c = a + b # create a list with size = len(a) + len(b)
>>> c[::2], c[1::2] = a, b # alternately insert the value
>>> c
[[0, 1], [5, 6], [2, 3], [7, 8]]
Below is the comparison of results with timeit
for the answers mentioned here (Python version: 2.7):
Using string slicing: 0.586 usec per loop
moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = a + b; c[::2], c[1::2] = a, b"
1000000 loops, best of 3: 0.586 usec per loop
Using itertools.chain()
: 1.89 usec per loop
moin@moin-pc:~$ python -m "timeit" -s "from itertools import chain; a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = list(chain(*zip(a, b)))"
1000000 loops, best of 3: 1.89 usec per loop
Using reduce()
: 0.829 usec per loop
moin@moin-pc:~$ python -m "timeit" -s "import operator; a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = reduce(operator.concat, zip(a, b))"
1000000 loops, best of 3: 0.829 usec per loop
Using list.extend()
: 0.824 usec per loop
moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]]; c=[]" "for pair in zip(a,b): c.extend(pair)"
1000000 loops, best of 3: 0.824 usec per loop
Using list.append()
twice: 1.04 usec per loop
moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]]; c=[]" "for a_element, b_element in zip(a, b): c.append(a_element); c.append(b_element)"
1000000 loops, best of 3: 1.04 usec per loop
Upvotes: 5
Reputation: 6053
Assuming len(a) == len(b) and you're adding them one by one in turn:
for i in range(len(a)):
c.append(a[i])
c.append(b[i])
However, I would recommend to use c = deque()
. Since deques are much quicker if you are making a lot of appends.
Upvotes: 0
Reputation: 311143
You could zip
the two lists and then reduce them to a flat list:
import operator
c = reduce(operator.concat, zip(a, b))
Upvotes: 2
Reputation: 1218
Consider:
merged = []
for a_element, b_element in zip(a, b):
merged.append(a_element)
merged.append(b_element)
Unless you have very stringent performance requirements, the simplest approach is the right approach.
Upvotes: 1
Reputation: 95908
Basic approach:
>>> a = [[0,1],[2,3]]
>>> b = [[5,6],[7,8]]
>>> c = []
>>> for pair in zip(a,b):
... c.extend(pair)
...
>>> c
[[0, 1], [5, 6], [2, 3], [7, 8]]
>>>
This breaks if the lengths aren't equal. But you can deal with that case as an exercise.
Upvotes: 6