To replace parts of a python list, you can use the slice assignment:
>>> array1 = ['A', 'B', 'C', 'D', 'E', 'F'] >>> array2 = ['G', 'H', 'I'] >>> array1[-1:] = array2 >>> array1 ['A', 'B', 'C', 'D', 'E', 'G', 'H', 'I']
You can use the slice assignment to replace any part of the list, including inserting lists in which you do not replace existing elements:
>>> array1[0:0] = ['1st', '2nd'] >>> array1 ['1st', '2nd', 'A', 'B', 'C', 'D', 'E', 'G', 'H', 'I']
Here the slice [0:0] selects the empty part of array1 and "replaces" it with new elements.
Martijn pieters
source share