python 3.x - Inserting list into another list using loops only: -


i'm using current version of python. need return copy of list1 list2 inserted @ position indicated index i.e if index value 2, list2 inserted list 1 @ position 2. can use for/while loops, range function & list_name.append (value) methods , lists cannot sliced. if list1 list1 = boom list2 = red , index value = 2, how return new list = boredom? have far:

list1 = ['b','o','o','m'] list2 = ['r','e','d'] index = 2 new_list = []  if index > len(list1):     new_list = list1 + list2     print (new_list) if index <= 0:     new_list = list2 + list1     print (new_list) 

an alternative approach padriac's - using 3 for loops:

list1 = ['b','o','o','m'] list2 = ['r','e','d']  n = 2 new_list = [] in range(n): # append list1 until insert point     new_list.append(list1[i]) in list2: # append of list2     new_list.append(i) in range(n, len(list1)): # append remainder of list1     new_list.append(list1[i]) 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -