Python – Cross Join every Kth segment
Given two lists, extract alternate elements at every Kth position.
Input : test_list1 = [4, 3, 8, 2, 6, 7], test_list2 = [5, 6, 7, 4, 3, 1], K = 3
Output : [4, 3, 8, 5, 6, 7, 2, 6, 7, 4, 3, 1]
Explanation : 4, 3, 8 after that 5, 6 from other list are extracted, and so on.
Input : test_list1 = [4, 3, 8, 2], test_list2 = [5, 6, 7, 4], K = 2
Output : [4, 3, 5, 6, 8, 2, 7, 4]
Explanation : 4, 3 after that 5, 6 from other list are extracted, and so on.
Method #1 : Using generator [yield] + loop
In this, we iterate for all elements, nested to get K elements at each pass from both list. The yield is used to dynamically return segments as they get processed.
Python3
# Python3 code to demonstrate working of# Cross Join every Kth segment# Using yield + loop# helper functiondef pair_merge(test_list1, test_list2, K): idx1 = 0 idx2 = 0 while(idx1 < len(test_list1)): # get K segments for i in range(K): yield test_list1[idx1] idx1 += 1 for i in range(K): yield test_list2[idx2] idx2 += 1# initializing liststest_list1 = [4, 3, 8, 2, 6, 7]test_list2 = [5, 6, 7, 4, 3, 1]# printing original listsprint("The original list 1 is : " + str(test_list1))print("The original list 2 is : " + str(test_list2))# initializing KK = 2# calling helper func. in generator expressionres = [ele for ele in pair_merge(test_list1, test_list2, K)]# printing resultprint("The cross joined list : " + str(res)) |
The original list 1 is : [4, 3, 8, 2, 6, 7] The original list 2 is : [5, 6, 7, 4, 3, 1] The cross joined list : [4, 3, 5, 6, 8, 2, 7, 4, 6, 7, 3, 1]
Method #2 : Using zip_longest() + list comprehension
In this, we get all the elements for each list using zip_longest, and list comprehension is used for the task of iteration in both list.
Python3
# Python3 code to demonstrate working of# Cross Join every Kth segment# Using zip_longest() + list comprehensionfrom itertools import zip_longest, chain# initializing liststest_list1 = [4, 3, 8, 2, 6, 7]test_list2 = [5, 6, 7, 4, 3, 1]# printing original listsprint("The original list 1 is : " + str(test_list1))print("The original list 2 is : " + str(test_list2))# initializing KK = 2# zip_longest to get segmentsres = [y for idx in zip(zip_longest(*[iter(test_list1)] * K), zip_longest(*[iter(test_list2)] * K)) for y in chain.from_iterable(idx) if y]# printing resultprint("The cross joined list : " + str(res)) |
The original list 1 is : [4, 3, 8, 2, 6, 7] The original list 2 is : [5, 6, 7, 4, 3, 1] The cross joined list : [4, 3, 5, 6, 8, 2, 7, 4, 6, 7, 3, 1]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course



