Skip to content Skip to sidebar Skip to footer

List Insertion In Python

Suppose I have two lists as follows:- a = [9,11,12,13] b = [0,5] Now, I want to create another list in which if the index equals to any element of b,then I want to insert -1 at t

Solution 1:

If I understood your question I believe that's what you're looking for.

a = [1, 2, 3, 4]
b = [0, 3]

def insert_at_indexes(li, indexes, value):
    for ind in indexes:
        li.insert(ind, value)

insert_at_indexes(a,b,-1)

Post a Comment for "List Insertion In Python"