PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] How to insert a list into a list at a specific location?

 May 13, 2022     append, insert, list, python     No comments   

Issue

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?


Solution

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)


Answered By - shaknoah
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing