@hacker_programer

Как в список добавить элементы другого списка?

Всем добрый день. Есть 2 списка. Как можно в один список засунуть другой список, но только по одному элементу?
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]
  • Вопрос задан
  • 59 просмотров
Решения вопроса 1
ScriptKiddo
@ScriptKiddo
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
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы