Python - Interleave two lists of different length
Last Updated :
27 Apr, 2023
Given two lists of different lengths, the task is to write a Python program to get their elements alternatively and repeat the list elements of the smaller list till the larger list elements get exhausted.
Examples:
Input : test_list1 = ['a', 'b', 'c'], test_list2 = [5, 7, 3, 0, 1, 8, 4]
Output : ['a', 5, 'b', 7, 'c', 3, 'a', 0, 'b', 1, 'c', 8, 'a', 4]
Explanation : Alternate elements from 1st list are printed in
cyclic manner once it gets exhausted. Then after exhaustion,
again 1st list starts from 'a', with elements left in 2nd list.
Input : test_list1 = [3, 8, 7], test_list2 = [5, 7, 3, 0, 1, 8]
Output : [3, 5, 8, 7, 7, 3, 3, 0, 8, 1, 7, 8]
Explanation : Alternate elements from 1st list are printed in cyclic manner
once it gets exhausted. 3, 5, 8, 7, 7, 3.. Then after exhaustion,
again 1st list starts from 3, with elements left in 2nd list
Method #1 : Using zip() + cycle() + list comprehension
In this, the repetition of elements in the smaller list is handled using cycle(), and joining is done using zip(). List comprehension performs the task of interleaving simultaneously.
Python3
# Python3 code to demonstrate working of
# Repetitive Interleave 2 lists
# Using zip() + cycle() + list comprehension
from itertools import cycle
# initializing lists
test_list1 = list('abc')
test_list2 = [5, 7, 3, 0, 1, 8, 4]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# zip() combining list, after Repetitiveness using cycle()
res = [ele for comb in zip(cycle(test_list1), test_list2) for ele in comb]
# printing result
print("The interleaved list : " + str(res))
OutputThe original list 1 is : ['a', 'b', 'c']
The original list 2 is : [5, 7, 3, 0, 1, 8, 4]
The interleaved list : ['a', 5, 'b', 7, 'c', 3, 'a', 0, 'b', 1, 'c', 8, 'a', 4]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #2 : Using chain() + zip() + cycle()
Most operations as above method, the only difference being interleaving task is performed using chain().
Python3
# Python3 code to demonstrate working of
# Repetitive Interleave 2 lists
# Using chain() + zip() + cycle()
from itertools import cycle, chain
# initializing lists
test_list1 = list('abc')
test_list2 = [5, 7, 3, 0, 1, 8, 4]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# zip() combining list, after Repetitiveness using cycle()
# chain() gets interleaved done
res = list(chain(*zip(cycle(test_list1), test_list2)))
# printing result
print("The interleaved list : " + str(res))
OutputThe original list 1 is : ['a', 'b', 'c']
The original list 2 is : [5, 7, 3, 0, 1, 8, 4]
The interleaved list : ['a', 5, 'b', 7, 'c', 3, 'a', 0, 'b', 1, 'c', 8, 'a', 4]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method 3: using the built-in function map().
Python3
from itertools import chain, cycle
# initializing lists
test_list1 = list('abc')
test_list2 = [5, 7, 3, 0, 1, 8, 4]
# Interleaving the lists
# using map() method
res = list(chain(*map(lambda x, y: [x, y], cycle(test_list1), test_list2)))
# printing result
print("The interleaved list : " + str(res))
OutputThe interleaved list : ['a', 5, 'b', 7, 'c', 3, 'a', 0, 'b', 1, 'c', 8, 'a', 4]
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Similar Reads
Python - Difference of Two Lists Including Duplicates
We are given two list we need find difference between two list. For example, a = [1, 2, 2, 3, 4] and b = [2, 3] we need to find difference of two list so that resultant output should be [1, 2, 4].Using a for loopA for loop can iterate through first list and remove matching elements from second list
2 min read
Python - Interleave multiple lists of same length
When we interleave multiple lists, we mix the items from each list so that they alternate in a new sequence. This is often done when we have lists of the same length and want to combine them, with each item from each list appearing one after another. In this article, we will explore interleaving mul
2 min read
Python - Chunked interleave of Lists
Sometimes, while working with Lists, we can have a problem to perform merge operation. The simpler version is easy to implement. But when it comes to implement a variation of it, i.e the case in which we need to interleave in chunks alternatively, it becomes a tougher task. Let's discuss certain way
2 min read
Add Elements of Two Lists in Python
Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Python - Difference of List keeping duplicates
The problem of finding difference between list, i.e removing elements that occur in one list and not in other is discussed before. But the usage of sets ignores duplicates and we sometimes, require to remove the exact elements that occur in lists. Lets discuss certain ways in which this task can be
6 min read
Iterate Over a List of Lists in Python
We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
2 min read
Difference between two Lists in Python
The difference between two lists in Python refers to the elements that are present in one list but not in the other. For example, finding the difference between lists a = [1, 2, 3, 4] and b = [3, 4, 5, 6] can result in [1, 2] by removing the common elements (3 and 4).Using setSet operations are most
3 min read
Python | Difference in Record Lists
Sometimes, while working with data, we may have a problem in which we require to find the difference records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
5 min read
Python | Average of two lists
The problem of finding a average values in a list is quite common. But sometimes this problem can be extended in two lists and hence becomes a modified problem. This article discusses shorthands by which this task can be performed easily. Letâs discuss certain ways in which this problem can be solve
5 min read
Transpose Elements of Two Dimensional List - Python
The task of transposing elements of a two-dimensional list in Python involves converting rows into columns and vice versa. This process rearranges the data structure so that the elements in the same position across different rows are grouped together in new sublists. For example, given the 2D list a
3 min read