How to add element in certain index in list?

So, i got a task, where i need to place elements in list in certain indexes, but there is no method to do that nor i can add in by using this:

    a = []
for i in range(int(input())):
    a.append(input())
a[int(input())] = input()

How do i get the programm to do it? Thanks!


Ответы (1 шт):

Автор решения: Ovsenka

It's how i understand:


N = int(input())
a = [0 for _ in range(N)]

for i in range(N):
    str_ = input(f"Enter str to insert: ")
    index = int(input(f"Enter index: "))
    a[index] = str_

This program uses list comprehensions and then fills that list

→ Ссылка