Answer the question
In order to leave comments, you need to log in
How to add elements of another list to a list?
Good afternoon everyone. There are 2 lists. How can I put another list into one list, but only one element at a time?
sp_with_follow = ['one', 'two', 'three', 'four', 'five']
res_list = ['1', 2, '3', 4, 5]
for i in range(len(sp_with_follow)):
for cv in sp_with_follow:
res_list.insert(4, cv)
print(res_list)
['1', 2, '3', 4, 'five', 'four', 'three', 'two', 'one', 'five', 'four', 'three', 'two', 'one', 'five', 'four', 'three', 'two', 'one', 'five', 'four', 'three', 'two', 'one', 'five', 'four', 'three', 'two', 'one', 5]
['1', 2, '3', 4, 'one', 5, '1', 2, '3', 4, 'two', 5, '1', 2, '3', 4, 'three', 5, '1', 2, '3', 4, 'four', 5, '1', 2, '3', 4, 'five', 5]
Answer the question
In order to leave comments, you need to log in
sp_with_follow = ['one', 'two', 'three', 'four', 'five']
res_list = ['1', 2, '3', 4, 5]
out = []
for el in sp_with_follow:
res_copy = res_list[:]
res_copy.insert(4,el)
out.extend(res_copy)
print(out)
['1', 2, '3', 4, 'one', 5, '1', 2, '3', 4, 'two', 5, '1', 2, '3', 4, 'three', 5, '1', 2, '3', 4, 'four', 5, '1', 2, '3', 4, 'five', 5]
Process finished with exit code 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question