Hi,
as already explained @
deanhystad, you perform a replacement using a slice operation. If you want to insert something, use the
insert
method:
>>> some_list = ['apple', 'banana', 'cherry']
>>> another_list = ['kiwi', 'mango']
>>> some_list.insert(1, another_list)
>>> some_list
['apple', ['kiwi', 'mango'], 'banana', 'cherry']
>>>
Please note that a) the list is modified in place and b) it is not exactly doing what you are looking for, as
banana
is still in the list (could be removed by the
pop
method of lists.
What you intend to do is possible, but I think only by a bit more slicing and rejoing the list:
>>> some_list = ['apple', 'banana', 'cherry']
>>> another_list = ['kiwi', 'mango']
>>> result = some_list[0:1]
>>> result.append(another_list)
>>> result
['apple', ['kiwi', 'mango']]
>>> result.append(some_list[-1])
>>> result
['apple', ['kiwi', 'mango'], 'cherry']
>>>
Maybe there's a more elegant to do that?
Regards, noisefloor