Python | Selective Merge list every Nth position
Sometimes, while working with Python list, we can come into a problem in which we need to perform merging in list. A simple merge of list is easier to perform. But sometimes, we need to handle variations in merge. One such can be merge one list with other at every Nth element. This particular problem can be faced in day-day programming and competitive programming. Let’s discuss certain way in which this task can be performed.
Method : Using loop +
extend() + iter() + next()
In this method, we use a brute force approach to tackle this problem. In this we convert a larger list into an iterator and then access it’s element using next(). The whole idea behind this is faster access of elements. Smaller list is checked for every N element.
The original list 1 is : [1, 4, 9, 10, 19, 65, 78, 23, 78] The original list 2 is : [8, 14, 50] The List after merge is : [1, 4, 8, 9, 10, 14, 19, 65, 50, 78, 23, 78]