This question already has an answer here:
-
How to elegantly interleave two lists of uneven length in python?
7 answers
I have two lists that could be not equal in lengths and I want to be able to interleave them. I want to be able to append the extra values in the longer list at the end of my interleaved list.I have this:
def interleave(xs,ys):
a=xs
b=ys
minlength=[len(a),len(b)]
extralist= list()
interleave= list()
for i in range((minval(minlength))):
pair=a[i],b[i]
interleave.append(pair)
flat=flatten(interleave)
c=a+b
if len(b)>len(a):
remainder=len(b)-len(a)
print(remainder)
for j in range(remainder,-1,-1):
extra=b[-j]
extralist.append(extra)
print(extralist)
if len(a)>len(b):
remainder=len(a)-len(b)
print(remainder)
for j in range(remainder,-1,-1):
extra=a[-j]
extralist.append(extra)
final=flat+extralist
return final
but if I test it:
>>> interleave([1,2,3], ["hi", "bye",True, False, 33])
[1, 'hi', 2, 'bye', 3, True]
>>>
The False and 33 don't appear. What is it that Im doing wrong?
EDIT: I found the solution to it. I indexed backwards :)
Here is a solution using itertools.zip_longest
and a list comprehension:
>>> from itertools import zip_longest
>>> a = [1, 2, 3]
>>> b = ["hi", "bye", True, False, 33]
>>> [y for x in zip_longest(a, b) for y in x]
[1, 'hi', 2, 'bye', 3, True, None, False, None, 33]
>>> [y for x in zip_longest(a, b) for y in x if y is not None]
[1, 'hi', 2, 'bye', 3, True, False, 33]
>>>
Edit:
If you don't want to use itertools
, I guess you could do:
>>> a = [1, 2, 3]
>>> b = ["hi", "bye", True, False, 33]
>>> items = sorted((a,b), key=len)
>>> out = []
>>> for x,y in enumerate(items[1]):
... out.append(y)
... try:
... out.append(items[0][x])
... except IndexError:
... out.extend(items[1][x+1:])
... break
...
>>> out
['hi', 1, 'bye', 2, True, 3, False, 33]
>>>
But I must say this method isn't as efficient as my first solution.
Edit 2:
Changing enumerate
to zip
reduces the efficiency again. However, if you must, then you can do this:
>>> a = [1, 2, 3]
>>> b = ["hi", "bye", True, False, 33]
>>> out = []
>>> items = sorted((a,b), key=len)
>>> for x,y in zip(range(len(items[1])), items[1]):
... out.append(y)
... try:
... out.append(items[0][x])
... except IndexError:
... out.extend(items[1][x+1:])
... break
...
>>> out
['hi', 1, 'bye', 2, True, 3, False, 33]
>>>
Another option
from itertools import chain
x = [1, 2, 3]
y = iter(["hi", "bye",True, False, 33])
list(chain.from_iterable(zip(x, y))) + list(y)
[1, 'hi', 2, 'bye', 3, True, False, 33]