I am trying to combine the contents of two lists, in order to later perform processing on the entire data set. I initially looked at the built in insert
functi
insert(i,j)
, where i
is the index and j
is what you want to insert, does not add as a list. Instead it adds as a list item:
array = ['the', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
array.insert(1,'brown')
The new array would be:
array = ['the', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
The extend
method of list object does this, but at the end of the original list.
addition.extend(array)
You can do the following using the slice syntax on the left hand side of an assignment:
>>> array = ['the', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
>>> array[1:1] = ['quick', 'brown']
>>> array
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
That's about as Pythonic as it gets!