Insert values of one list into another at every n position
up vote
2
down vote
favorite
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.
python
add a comment |
up vote
2
down vote
favorite
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.
python
So did you try adapting the examples you found to your specific needs? What happened?
– jonrsharpe
Nov 11 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 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.
– Ayxan
Nov 11 at 16:58
@Ares Yup, I know how to use SO ;) Still reviewing the answers as they're all pretty good.
– ls22
Nov 11 at 17:16
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
python
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.
python
python
edited Nov 11 at 16:15
eyllanesc
70.8k93052
70.8k93052
asked Nov 11 at 16:12
ls22
235
235
So did you try adapting the examples you found to your specific needs? What happened?
– jonrsharpe
Nov 11 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 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.
– Ayxan
Nov 11 at 16:58
@Ares Yup, I know how to use SO ;) Still reviewing the answers as they're all pretty good.
– ls22
Nov 11 at 17:16
add a comment |
So did you try adapting the examples you found to your specific needs? What happened?
– jonrsharpe
Nov 11 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 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.
– Ayxan
Nov 11 at 16:58
@Ares Yup, I know how to use SO ;) Still reviewing the answers as they're all pretty good.
– ls22
Nov 11 at 17:16
So did you try adapting the examples you found to your specific needs? What happened?
– jonrsharpe
Nov 11 at 16:19
So did you try adapting the examples you found to your specific needs? What happened?
– jonrsharpe
Nov 11 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 at 16:22
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 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.
– Ayxan
Nov 11 at 16:58
@ls22 if your question was answered, please make sure to accept an answer for further references. You might ask for further clarification otherwise.
– Ayxan
Nov 11 at 16:58
@Ares Yup, I know how to use SO ;) Still reviewing the answers as they're all pretty good.
– ls22
Nov 11 at 17:16
@Ares Yup, I know how to use SO ;) Still reviewing the answers as they're all pretty good.
– ls22
Nov 11 at 17:16
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
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, anda.insert(len(a),
is equivalent to
x)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']
add a comment |
up vote
1
down vote
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']
add a comment |
up vote
1
down vote
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)
add a comment |
up vote
1
down vote
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']
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 at 17:32
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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, anda.insert(len(a),
is equivalent to
x)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']
add a comment |
up vote
0
down vote
accepted
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, anda.insert(len(a),
is equivalent to
x)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']
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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, anda.insert(len(a),
is equivalent to
x)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']
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, anda.insert(len(a),
is equivalent to
x)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']
edited Nov 11 at 16:52
answered Nov 11 at 16:35
Ayxan
1,422115
1,422115
add a comment |
add a comment |
up vote
1
down vote
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']
add a comment |
up vote
1
down vote
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']
add a comment |
up vote
1
down vote
up vote
1
down vote
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']
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']
answered Nov 11 at 16:23
jpp
87.2k194999
87.2k194999
add a comment |
add a comment |
up vote
1
down vote
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)
add a comment |
up vote
1
down vote
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)
add a comment |
up vote
1
down vote
up vote
1
down vote
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)
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)
answered Nov 11 at 16:38
Omkar Nath Singh
1,7492925
1,7492925
add a comment |
add a comment |
up vote
1
down vote
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']
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 at 17:32
add a comment |
up vote
1
down vote
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']
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 at 17:32
add a comment |
up vote
1
down vote
up vote
1
down vote
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']
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']
edited 9 hours ago
answered Nov 11 at 16:42
Co.Q
112
112
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 at 17:32
add a comment |
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 at 17:32
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 at 17:32
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 at 17:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250626%2finsert-values-of-one-list-into-another-at-every-n-position%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So did you try adapting the examples you found to your specific needs? What happened?
– jonrsharpe
Nov 11 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 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.
– Ayxan
Nov 11 at 16:58
@Ares Yup, I know how to use SO ;) Still reviewing the answers as they're all pretty good.
– ls22
Nov 11 at 17:16