>>> l1 = [1,2,3] >>> l2 = ['a','b','c','d','e','f','g'] >>> [i for i in itertools.chain(*itertools.izip_longest(l1,l2)) if i is not None] [1, 'a', 2, 'b', 3, 'c', 'd', 'e', 'f', 'g']
To allow the inclusion of None values ββin lists, you can use the following modification:
>>> from itertools import chain, izip_longest >>> l1 = [1, None, 2, 3] >>> l2 = ['a','b','c','d','e','f','g'] >>> sentinel = object() >>> [i for i in chain(*izip_longest(l1, l2, fillvalue=sentinel)) if i is not sentinel] [1, 'a', None, 'b', 2, 'c', 3, 'd', 'e', 'f', 'g']
inspectorG4dget
source share