how to interleaving lists
Posted
by
user2829177
on Stack Overflow
See other posts from Stack Overflow
or by user2829177
Published on 2013-11-10T15:50:39Z
Indexed on
2013/11/10
15:52 UTC
Read the original article
Hit count: 175
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:
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(c)-len(a)
for j in range(-remainder):
extra=remainder[j]
extralist.append(extra)
if len(a)>len(b):
remainder=len(c)-len(b)
for j in range(-remainder):
extra=remainder[j]
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?
© Stack Overflow or respective owner