Python | Merge two lists alternatively
Given two lists, write a Python program to merge the given lists in an alternative fashion, provided that the two lists are of equal length.
Examples:
Input : lst1 = [1, 2, 3]
lst2 = ['a', 'b', 'c']
Output : [1, 'a', 2, 'b', 3, 'c']
Input : lst1 = ['name', 'alice', 'bob']
lst2 = ['marks', 87, 56]
Output : ['name', 'marks', 'alice', 87, 'bob', 56]
Method #1 : List comprehension
# Python3 program to merge two lists # alternatively def countList(lst1, lst2): return [sub[item] for item in range(len(lst2)) for sub in [lst1, lst2]] # Driver code lst1 = [1, 2, 3] lst2 = ['a', 'b', 'c'] print(countList(lst1, lst2)) |
chevron_right
filter_none
Output:
[1, 'a', 2, 'b', 3, 'c']
There is an alternative to use list comprehension with zip() as given below –
def countList(lst1, lst2): return [item for pair in zip(lst1, lst2 + [0]) for item in pair] |
chevron_right
filter_none
Method #2 : Using itertools.cycle()
# Python3 program to merge two lists # alternatively from itertools import cycle def countList(lst1, lst2): iters = [iter(lst1), iter(lst2)] return list(iter.__next__() for iter in cycle(iters)) # Driver code lst1 = [1, 2, 3] lst2 = ['a', 'b', 'c'] print(countList(lst1, lst2)) |
chevron_right
filter_none
Output:
[1, 'a', 2, 'b', 3, 'c']
Method #3 : Using reduce()
# Python3 program to merge two lists # alternatively import operator from functools import reduce def countList(lst1, lst2): return reduce(operator.add, zip(lst1, lst2)) # Driver code lst1 = [1, 2, 3] lst2 = ['a', 'b', 'c'] print(countList(lst1, lst2)) |
chevron_right
filter_none
Output:
(1, 'a', 2, 'b', 3, 'c')
Method #4 : Using numpy module
# Python3 program to merge two lists # alternatively import numpy as np def countList(lst1, lst2): return np.array([[i, j] for i, j in zip(lst1, lst2)]).ravel() # Driver code lst1 = [1, 2, 3] lst2 = ['a', 'b', 'c'] print(countList(lst1, lst2)) |
chevron_right
filter_none
Output:
['1' 'a' '2' 'b' '3' 'c']
Recommended Posts:
- Python | Merge key value lists
- Python | Merge corresponding sublists from two different lists
- Python | Merge two lists into list of tuples
- Python | Merge two list of lists according to first element
- Python | Merge overlapping part of lists
- Python | Merge List with common elements in a List of Lists
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
- Python | Merge Python key values to list
- Python | Merge list elements
- Python PIL | Image.merge() method
- Python | Merge two text files
- Python | List value merge in dictionary
- Python Program for Merge Sort
- Python | Merge elements of sublists
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.



