The function below inserts a list into another list at a specified location.
What's the best way to remove the middle brackets from the output while leaving the outer brackets on?
def insert(list1, list2, index):
pos = 0
new_list = []
if index > len(list1):
index = len(list1)
elif index < 0:
index = 0
for pos in range(0, index):
new_list.append(list1[pos])
#for pos in range(1):
new_list.append(list2)
while index != len(list1):
new_list.append(list1[index])
index += 1
return new_list
list1 = ["b", "o", "o", "m"]
list2 = ["r", "e", "d"]
index = 2
new_list = insert(list1, list2, index)
print(new_list)
Output:
['b', 'o', ['r', 'e', 'd'], 'o', 'm']