+ 20

Merge 2 lists in python

Is there an elegant way to [0,1,2,3], [4,5,6] - >[0,4,1,5,2,6,3]?

10th Jul 2020, 9:51 AM
Oma Falk
Oma Falk - avatar
53 ответов
+ 24
#Oneliner a = [0,1,2,3,5,6] b = [4,5,6] print(sum([[i, j] for i, j in zip(a, b)] + [a[len(b):]] or [b[len(a):]], []))
10th Jul 2020, 12:20 PM
Louis
Louis - avatar
+ 14
This would be an inelegant way I suppose... a, b = [0, 1, 2, 3, 4], [5, 6, 7] c = [] for i in range(min((len(a), len(b)))): c.append(a[i]) c.append(b[i]) c.extend(a[i+1:] or b[i+1]) print(c)
10th Jul 2020, 10:23 AM
HonFu
HonFu - avatar
+ 9
Yeah, the efficiency problem remains: We create stuff we don't want.
10th Jul 2020, 11:29 AM
HonFu
HonFu - avatar
+ 8
I think this is most pythonic: [x for x in chain.from_iterable(zip_longest(a, b)) if x is not None] I also have another solution with pop() that consumes the original lists. https://code.sololearn.com/ctGh1XLDz1nt/?ref=app
10th Jul 2020, 3:21 PM
Tibor Santa
Tibor Santa - avatar
+ 8
10th Jul 2020, 8:02 PM
Saeed Alqassabi
Saeed Alqassabi - avatar
+ 5
There's itertools.zip_longest, which fills up the shorter iterables with None.
10th Jul 2020, 11:06 AM
HonFu
HonFu - avatar
+ 5
That will fill _ into the list, 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥. Sure, you can filter after that, but you're then losing elegance.
10th Jul 2020, 11:11 AM
HonFu
HonFu - avatar
+ 5
What mean is: When you look at it, you should feel like damn, THAT'S how you do it, why didn't I think of that?
10th Jul 2020, 12:52 PM
HonFu
HonFu - avatar
+ 5
Not elegant but the most efficient and readable I could find. https://code.sololearn.com/c501xOJcSnQY/?ref=app [Edit] Oma Falk #len(l2) >= len(l1) res = [n for x in zip(l1,l2) for n in x] + l2[len(l1):] I found the way to flatten a tuple here: https://stackoverflow.com/questions/3204245/how-do-i-convert-a-tuple-of-tuples-to-a-one-dimensional-list-using-list-comprehe
10th Jul 2020, 11:08 PM
Kevin ★
+ 4
But how do you get to the *end* result?
10th Jul 2020, 11:15 AM
HonFu
HonFu - avatar
+ 4
Yeah, as I said, list full of Nones.
10th Jul 2020, 11:21 AM
HonFu
HonFu - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥, I mean, your version creates a list that has Nones in them, but then we make another list where we take the Nones out.
10th Jul 2020, 11:31 AM
HonFu
HonFu - avatar
+ 4
a = [0,1,2,3] b = [4,5,6] c = a + b c[1:len(b)*2:2] = b c[:len(b)*2+1:2] = a[:len(b)+1] print(c) # Put bigger list in "a" and smaller list in"b"
10th Jul 2020, 12:20 PM
Valmob101
Valmob101 - avatar
+ 4
Speed comparison between different known methods mentioned in this thread: https://code.sololearn.com/c39kIttuOIgd/?ref=app
10th Jul 2020, 12:52 PM
Valmob101
Valmob101 - avatar
+ 4
Quick way,elegant way Just Define a merge function lst1 = [0,1,2,3] lst2 = [4,5,6] def mergeList(a,b): return a + b fulLst = mergeList(lst1,lst2) print(fulLst)
10th Jul 2020, 2:27 PM
TEMPER.xi
TEMPER.xi - avatar
+ 4
not very elegant, but there's this... print(sum(list(zip(a+b,b+a)), ())[0:len(a+b)])
10th Jul 2020, 3:26 PM
madeline
madeline - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Sami Khan my lists are not equal length 😒
10th Jul 2020, 10:06 AM
Oma Falk
Oma Falk - avatar
+ 3
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I remember a long zip in itertools🤔🤔 cant remember but that woukd make your zip variant perfect
10th Jul 2020, 10:56 AM
Oma Falk
Oma Falk - avatar
10th Jul 2020, 11:26 AM
Oma Falk
Oma Falk - avatar
+ 3
This might be a bit late but....pip jnstall "more-itertools" then :- import more_itertools list1 = [1, 3, 5, 7] list2 = [2, 4, 6, 8] print(list(more_itertools.roundrobin(list1, list2))) output = [1, 2, 3, 4, 5, 6, 7, 8]
10th Jul 2020, 9:31 PM
rodwynnejones
rodwynnejones - avatar