Python | Interleave multiple lists of same length

Given lists of same length, write a Python program to store alternative elements of given lists in a new list.

Let’s discuss certain ways in which this can be performed.

Method #1 : Using `map() + list comprehension`
`map()` is used to handle the interleave of lists and the task of insertion at alternate is performed by the list comprehension part of the shorthand code. Only works in Python 2.

 `# Python2 code to demonstrate  ` `# to interleave lists ` `# using map() + list comprehension ` ` `  `# initializing lists  ` `test_list1 ``=` `[``1``, ``4``, ``5``] ` `test_list2 ``=` `[``3``, ``8``, ``9``] ` ` `  `# printing original lists ` `print` `(``"Original list 1 : "` `+` `str``(test_list1)) ` `print` `(``"Original list 2 : "` `+` `str``(test_list2)) ` ` `  `# using map() + list comprehension ` `# to interleave lists ` `res ``=` `[i ``for` `j ``in` `map``(``None``, test_list1, test_list2)   ` `                       ``for` `i ``in` `j ``if` `i ``is` `not` `None``] ` ` `  `# printing result ` `print` `(``"The interleaved list is : "` `+` `str``(res)) `

Output:

```Original list 1 : [1, 4, 5]
Original list 2 : [3, 8, 9]
The interleaved list is : [1, 3, 4, 8, 5, 9]
```

Method #2 : Using List slicing
Power of list slicing of python can also be used to perform this particular task. We first extend one list to another and then allow the original list to desired alternate indices of the resultant list.

 `# Python3 code to demonstrate  ` `# to interleave lists ` `# using list slicing ` ` `  `# initializing lists  ` `test_list1 ``=` `[``1``, ``4``, ``5``] ` `test_list2 ``=` `[``3``, ``8``, ``9``] ` ` `  `# printing original lists ` `print` `(``"Original list 1 : "` `+` `str``(test_list1)) ` `print` `(``"Original list 2 : "` `+` `str``(test_list2)) ` ` `  `# using list slicing ` `# to interleave lists ` `res ``=` `test_list1 ``+` `test_list2 ` `res[::``2``] ``=` `test_list1 ` `res[``1``::``2``] ``=` `test_list2 ` ` `  `# printing result ` `print` `(``"The interleaved list is : "` `+` `str``(res)) `

Output:

```Original list 1 : [1, 4, 5]
Original list 2 : [3, 8, 9]
The interleaved list is : [1, 3, 4, 8, 5, 9]
```

Method #3 : Using `itertools.chain() + zip()`
`zip()` can be used to link both the lists and then `chain()` can used to perform the alternate append of the elements as desired. This is the most efficient method to perform this task.

 `# Python3 code to demonstrate  ` `# to interleave lists ` `# using zip() + itertools.chain() ` `import` `itertools ` ` `  `# initializing lists  ` `test_list1 ``=` `[``1``, ``4``, ``5``] ` `test_list2 ``=` `[``3``, ``8``, ``9``] ` ` `  `# printing original lists ` `print` `(``"Original list 1 : "` `+` `str``(test_list1)) ` `print` `(``"Original list 2 : "` `+` `str``(test_list2)) ` ` `  `# using zip() + itertools.chain() ` `# to interleave lists ` `res ``=` `list``(itertools.chain(``*``zip``(test_list1, test_list2))) ` ` `  `# printing result ` `print` `(``"The interleaved list is : "` `+` `str``(res)) `

Output:

```Original list 1 : [1, 4, 5]
Original list 2 : [3, 8, 9]
The interleaved list is : [1, 3, 4, 8, 5, 9]
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.