Home > Software engineering >  How to insert a list into a list at a specific location?
How to insert a list into a list at a specific location?

Time:10-25

I have a list as following

List_a = [1, 0, 0, 0, 1, 1]

I want to insert [0, 3] into index 1.

How it should look like:

New_List_a = [1, [0,3], 0, 0, 1, 1]

Any ideas?

CodePudding user response:

List_a = [1, 0, 0, 0, 1, 1]
List_a[1] = [List_a[1], 3]
print(List_a)  # [1, [0, 3], 0, 0, 1, 1]

CodePudding user response:

def add(list1, index, value):
     list1[index] = [List1[index], value]
     return list1

CodePudding user response:

the basic approach is

List_a = [1, 0, 0, 0, 1, 1]
# New_List_a = [1, [0,3], 0, 0, 1, 1]
l=[]
l.append(List_a[1])

for i in range(0,len(List_a)):
    if(i==1): # real signature unknown
        l.append(3)
        List_a[i]=l
        break

or you can also do this

   List_a = [1, 0, 0, 0, 1, 1]
   List_a[1] = [List_a[1], 3]     
   print(List_a)
  • Related