2

Lots of examples on SO for inserting the same single value into another list at n positions, but can't find anything demonstrating the following:

Take the following lists:

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] 
list2 = ['this', 'that', 'the', 'other']

Insert each value of list2 into list1 every 2 positions to return:

['a', 'b', 'this', 'c', 'd', 'that', 'e', 'f', 'the', 'g', 'h', 'other']

Alternatively just create a 3rd list with same result.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ls22
  • 43
  • 6
  • So did you try adapting the examples you found to your specific needs? What happened? – jonrsharpe Nov 11 '18 at 16:19
  • Yes I have a .py file which is a mess of attempts that isn't worth tidying up to post anything in here. This morning I thought this would be trivial. Seemingly not! – ls22 Nov 11 '18 at 16:22
  • @ls22 if your question was answered, please make sure to accept an answer for further references. You might ask for further clarification otherwise. – Aykhan Hagverdili Nov 11 '18 at 16:58
  • @Ares Yup, I know how to use SO ;) Still reviewing the answers as they're all pretty good. – ls22 Nov 11 '18 at 17:16

4 Answers4

1

You can use zip with a list comprehension and chunk list1 via this recipe:

from itertools import chain

def chunks(L, n):
    """Yield successive n-sized chunks from L."""
    for i in range(0, len(L), n):
        yield L[i:i + n]

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] 
list2 = ['this', 'that', 'the', 'other']

zipper = zip(chunks(list1, 2), list2)
res = list(chain.from_iterable((x, y, z) for (x, y), z in zipper))

['a', 'b', 'this', 'c', 'd', 'that', 'e', 'f', 'the', 'g', 'h', 'other']
jpp
  • 159,742
  • 34
  • 281
  • 339
1

You can try following solution, simple and clean:

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] 
list2 = ['this', 'that', 'the', 'other']

i=2
j=0
while(j<len(list1) and j<len(list2)):
    list1.insert(i, list2[j])
    i=i+3
    j=j+1

print(list1)
Omkar Nath Singh
  • 3,375
  • 2
  • 15
  • 34
1

you can try the following code:

def insert_list(old_list, new_list, n):
    for i, val in enumerate(new_list):
        old_list.insert(n+i*3, val)
    return old_list

Test:

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] 
list2 = ['this', 'that', 'the', 'other']
print(insert_list(list1, list2, 2))

Output:

['a', 'b', 'this', 'c', 'd', 'that', 'e', 'f', 'the', 'g', 'h', 'other']
Co.Q
  • 11
  • 2
  • I thought this answer was most elegant and ended up adapting it for my use case. I would have accepted the answer but just writing 'such?' isn't that useful for others who find this question in future. Keep up the good answers and consider adding a bit more context next time :) – ls22 Nov 11 '18 at 17:32
0

insert can be used to insert a single value to the list

Let's see what the documentation says about insert:

list.insert(i, x)

Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

Inserts before the given index. Let's have a look at an example implementation keeping that in mind:

def ins_n(lst1, lst2, n):            # insert every n elements
  indx1 = n
  indx2 = 0
  while(indx1 <= len(lst1)):
    lst1.insert(indx1, lst2[indx2])
    indx1 += 1 + n                   # add `1` because we insert before the given index
    indx2 += 1
  return lst1

Test it with your example lists and 2:

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] 
list2 = ['this', 'that', 'the', 'other']

print(ins_n(list1, list2, 2))

Output:

['a', 'b', 'this', 'c', 'd', 'that', 'e', 'f', 'the', 'g', 'h', 'other']
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93