H
H
hacker_programmer2020-06-08 19:00:39
Python
hacker_programmer, 2020-06-08 19:00:39

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)


Conclusion:
['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]


But how to get the output
['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

1 answer(s)
S
ScriptKiddo, 2020-06-08
@hacker_programmer

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)

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 question

Ask a Question

731 491 924 answers to any question