98
votes

I have two lists, the first of which is guaranteed to contain exactly one more item than the second. I would like to know the most Pythonic way to create a new list whose even-index values come from the first list and whose odd-index values come from the second list.

# example inputs
list1 = ['f', 'o', 'o']
list2 = ['hello', 'world']

# desired output
['f', 'hello', 'o', 'world', 'o']

This works, but isn't pretty:

list3 = []
while True:
    try:
        list3.append(list1.pop(0))
        list3.append(list2.pop(0))
    except IndexError:
        break

How else can this be achieved? What's the most Pythonic approach?

21
Not a duplicate! The accepted answer in the above-linked article produces a list of tuples, not a single, merged list. - Paul Sasik
@Paul: Yes, the accepted answer does not give the complete solution. Read the comments and the other answers. The question is basically the same and the other solutions can be applied here. - Felix Kling
@Felix: I respectfully disagree. It is true, the questions are in the same neighborhood but not really duplicates. As vague proof take a look at the potential answers here and compare with the other question. - Paul Sasik

21 Answers

130
votes

Here's one way to do it by slicing:

>>> list1 = ['f', 'o', 'o']
>>> list2 = ['hello', 'world']
>>> result = [None]*(len(list1)+len(list2))
>>> result[::2] = list1
>>> result[1::2] = list2
>>> result
['f', 'hello', 'o', 'world', 'o']
52
votes

There's a recipe for this in the itertools documentation:

from itertools import cycle, islice

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))

EDIT:

For python's version greater than 3:

from itertools import cycle, islice

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = cycle(iter(it).__next__ for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))
36
votes
import itertools
print [x for x in itertools.chain.from_iterable(itertools.izip_longest(list1,list2)) if x]

I think this is the most pythonic way of doing it.

31
votes

In Python 2, this should do what you want:

>>> iters = [iter(list1), iter(list2)]
>>> print list(it.next() for it in itertools.cycle(iters))
['f', 'hello', 'o', 'world', 'o']
20
votes

Without itertools and assuming l1 is 1 item longer than l2:

>>> sum(zip(l1, l2+[0]), ())[:-1]
('f', 'hello', 'o', 'world', 'o')

In python 2, using itertools and assuming that lists don't contain None:

>>> filter(None, sum(itertools.izip_longest(l1, l2), ()))
('f', 'hello', 'o', 'world', 'o')
13
votes

I know the questions asks about two lists with one having one item more than the other, but I figured I would put this for others who may find this question.

Here is Duncan's solution adapted to work with two lists of different sizes.

list1 = ['f', 'o', 'o', 'b', 'a', 'r']
list2 = ['hello', 'world']
num = min(len(list1), len(list2))
result = [None]*(num*2)
result[::2] = list1[:num]
result[1::2] = list2[:num]
result.extend(list1[num:])
result.extend(list2[num:])
result

This outputs:

['f', 'hello', 'o', 'world', 'o', 'b', 'a', 'r'] 
11
votes

If both lists have equal length, you can do:

[x for y in zip(list1, list2) for x in y]

As the first list has one more element, you can add it post hoc:

[x for y in zip(list1, list2) for x in y] + [list1[-1]]
6
votes

Here's a one liner that does it:

list3 = [ item for pair in zip(list1, list2 + [0]) for item in pair][:-1]

2
votes

Here's a one liner using list comprehensions, w/o other libraries:

list3 = [sub[i] for i in range(len(list2)) for sub in [list1, list2]] + [list1[-1]]

Here is another approach, if you allow alteration of your initial list1 by side effect:

[list1.insert((i+1)*2-1, list2[i]) for i in range(len(list2))]
2
votes

This one is based on Carlos Valiente's contribution above with an option to alternate groups of multiple items and make sure that all items are present in the output :

A=["a","b","c","d"]
B=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

def cyclemix(xs, ys, n=1):
    for p in range(0,int((len(ys)+len(xs))/n)):
        for g in range(0,min(len(ys),n)):
            yield ys[0]
            ys.append(ys.pop(0))
        for g in range(0,min(len(xs),n)):
            yield xs[0]
            xs.append(xs.pop(0))

print [x for x in cyclemix(A, B, 3)]

This will interlace lists A and B by groups of 3 values each:

['a', 'b', 'c', 1, 2, 3, 'd', 'a', 'b', 4, 5, 6, 'c', 'd', 'a', 7, 8, 9, 'b', 'c', 'd', 10, 11, 12, 'a', 'b', 'c', 13, 14, 15]
2
votes

Might be a bit late buy yet another python one-liner. This works when the two lists have equal or unequal size. One thing worth nothing is it will modify a and b. If it's an issue, you need to use other solutions.

a = ['f', 'o', 'o']
b = ['hello', 'world']
sum([[a.pop(0), b.pop(0)] for i in range(min(len(a), len(b)))],[])+a+b
['f', 'hello', 'o', 'world', 'o']
1
votes

My take:

a = "hlowrd"
b = "el ol"

def func(xs, ys):
    ys = iter(ys)
    for x in xs:
        yield x
        yield ys.next()

print [x for x in func(a, b)]
1
votes
def combine(list1, list2):
    lst = []
    len1 = len(list1)
    len2 = len(list2)

    for index in range( max(len1, len2) ):
        if index+1 <= len1:
            lst += [list1[index]]

        if index+1 <= len2:
            lst += [list2[index]]

    return lst
1
votes
from itertools import chain
list(chain(*zip('abc', 'def')))  # Note: this only works for lists of equal length
['a', 'd', 'b', 'e', 'c', 'f']
0
votes

Stops on the shortest:

def interlace(*iters, next = next) -> collections.Iterable:
    """
    interlace(i1, i2, ..., in) -> (
        i1-0, i2-0, ..., in-0,
        i1-1, i2-1, ..., in-1,
        .
        .
        .
        i1-n, i2-n, ..., in-n,
    )
    """
    return map(next, cycle([iter(x) for x in iters]))

Sure, resolving the next/__next__ method may be faster.

0
votes

This is nasty but works no matter the size of the lists:

list3 = [element for element in list(itertools.chain.from_iterable([val for val in itertools.izip_longest(list1, list2)])) if element != None]
0
votes

Multiple one-liners inspired by answers to another question:

import itertools

list(itertools.chain.from_iterable(itertools.izip_longest(list1, list2, fillvalue=object)))[:-1]

[i for l in itertools.izip_longest(list1, list2, fillvalue=object) for i in l if i is not object]

[item for sublist in map(None, list1, list2) for item in sublist][:-1]
0
votes

How about numpy? It works with strings as well:

import numpy as np

np.array([[a,b] for a,b in zip([1,2,3],[2,3,4,5,6])]).ravel()

Result:

array([1, 2, 2, 3, 3, 4])
0
votes

An alternative in a functional & immutable way (Python 3):

from itertools import zip_longest
from functools import reduce

reduce(lambda lst, zipped: [*lst, *zipped] if zipped[1] != None else [*lst, zipped[0]], zip_longest(list1, list2),[])
-1
votes

I'd do the simple:

chain.from_iterable( izip( list1, list2 ) )

It'll come up with an iterator without creating any additional storage needs.

-2
votes

I'm too old to be down with list comprehensions, so:

import operator
list3 = reduce(operator.add, zip(list1, list2))