# Python | Merge two lists alternatively

Last Updated : 21 Apr, 2023

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

 `# 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))`

Output:

`[1, 'a', 2, 'b', 3, 'c']`

There is an alternative to use list comprehension with zip() as given below –

## Python3

 `def` `countList(lst1, lst2):` `    ``return` `[item ``for` `pair ``in` `zip``(lst1, lst2 ``+` `[``0``])` `                                 ``for` `item ``in` `pair]`

Method #2 : Using itertools.cycle()

## Python3

 `# 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))`

Output:

`[1, 'a', 2, 'b', 3, 'c']`

Method #3 : Using reduce()

## Python3

 `# 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))`

Output:

`(1, 'a', 2, 'b', 3, 'c')`

Method #4 : Using numpy module

## Python3

 `# 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))`

Output:

`['1' 'a' '2' 'b' '3' 'c']`

#### Approach

this approach is a recursive approach. In this approach, a function is defined that takes two lists as arguments. The function first checks if either of the lists is empty. If one of the lists is empty, the function returns the other list. If both lists have elements, the function concatenates the first element of the first list with the first element of the second list, and then calls itself recursively with the second list as the first argument and the remaining elements of the first list as the second argument. The function continues this process until both lists are empty, at which point the concatenated list is returned.

#### Algorithm

Define a function “merge_alternatively_rec” that takes two lists as arguments.
Check if either of the lists is empty. If so, return the other list.
Otherwise, concatenate the first element of the first list with the result of calling “merge_alternatively_rec” with the second list as the first argument and the rest of the first list as the second argument.
Return the concatenated list.

## Python3

 `def` `merge_alternatively(lst1, lst2):` `    ``if` `not` `lst1:` `        ``return` `lst2` `    ``if` `not` `lst2:` `        ``return` `lst1` `    ``return` `[lst1[``0``], lst2[``0``]] ``+` `merge_alternatively(lst1[``1``:], lst2[``1``:])` `lst1 ``=` `[``1``, ``2``, ``3``]` `lst2 ``=` `[``'a'``, ``'b'``, ``'c'``]` `print``(merge_alternatively(lst1, lst2))`

Output

`[1, 'a', 2, 'b', 3, 'c']`

Time complexity: O(n), where n is the length of the longer input list.
Space complexity: O(n), where n is the length of the longer input list.

METHOD 6: Using a loop and append() method: In this approach, we use a loop and the append() method to merge two lists alternatively. We iterate over the length of the smaller list and add the elements of both lists at the current index. After the smaller list is exhausted, we append the remaining elements of the long list to the merged list.

1. Initialize an empty list named merged_lst to hold the merged list.
2. Calculate the minimum length between lst1 and lst2 using the min() function.
3. Use a for loop to iterate over both lists up to the minimum length.
4. In each iteration, append the element at the current index from lst1 to merged_lst and then the element at the same index from lst2.
5. After the loop, use the slicing operator to append any remaining elements from lst1 and lst2 to merged_lst.
6. Print the merged_lst.

## Python3

 `lst1 ``=` `[``1``, ``2``, ``3``]` `lst2 ``=` `[``'a'``, ``'b'``, ``'c'``]`   `merged_lst ``=` `[]` `min_len ``=` `min``(``len``(lst1), ``len``(lst2))`   `for` `i ``in` `range``(min_len):` `    ``merged_lst.append(lst1[i])` `    ``merged_lst.append(lst2[i])`   `merged_lst ``+``=` `lst1[min_len:] ``+` `lst2[min_len:]`   `print``(merged_lst)`

Output

`[1, 'a', 2, 'b', 3, 'c']`

Time Complexity: O(n), where n is the length of the merged list.

Space Complexity: O(n), where n is the length of the merged list

Previous Article
Next Article
Article Tags :
Practice Tags :