Slicing and Splicing
A slice is a section of an array or list. Usually it’s a contiguous section (but see the step parameter for an exception to that rule). When you use the syntax or method calls provided by the language to get a slice of an array, it’s basically a new array or list that’s shallow copy of that section of the original array/list.
Splicing is inserting a sequence of items in an array or list, possibly replacing a given section (i.e. replacing a slice), but possibly just inserting them between existing items.
The same slicing syntax works in the same way for strings in both languages, but splicing doesn’t: both languages treat strings as immutable.
Slice Syntax in Python
In Python, to get a slice of a list, you use the following syntax: arrayName[start:stop:step]. Say it with me now: “start stop step”. I find that the alliteration makes this easy to remember this way, and slice syntax also uses the same parameters as Python’s range() function.
start: The index of the first item to include in the slice.
stop: The index of the first item to *not* include in the slice.
step: The number of items from one included item to the next. This defaults to 1. If it’s 2, you’re taking every other item.
Here’s an example:
>> arr = ["red", "green", "blue", "alpha"] >>> print(arr[1:3:1]) ['green', 'blue']
We start with item 1 (remember, lists are zero-based), and stop right before item 3. So our slice includes items 1 (“green”) and 2 (“blue”).
Since we’re using a step of 1, we can omit that parameter:
>> arr = ["red", "green", "blue", "alpha"] >>> print(arr[1:3]) ['green', 'blue']
To get a slice of an array, you use the slice() method. It uses start and end parameters, which function the same as start and stop in Python slices:
>> let arr = ["red", "green", "blue", "alpha"] >> arr Array(4) [ "red", "green", "blue", "alpha" ] >> arr.slice(1, 3) Array [ "green", "blue" ]
Splicing in Python
As with slices, splicing in Python is done with special syntax. It’s the same syntax as slices, except you add = afterward to replace the slice with a new sequence:
>>> arr ['red', 'green', 'blue', 'alpha'] >>> arr[1:3] = ["orange", "yellow", "cyan"] >>> arr ['red', 'orange', 'yellow', 'cyan', 'alpha']
A Python programmer might not usually use the term “splicing”, since there’s no splice() method. The official Python documentation just calls it “assigning to a slice”.
You can also insert the elements of an array into another array using the spread (…) operator (similar to * in Python):
>> let arr = [8, 6, 0, 9] >> let arr2 = [7, 5, 3] >> arr.splice(2, 0, ...arr2) Array  >> arr Array(7) [ 8, 6, 7, 5, 3, 0, 9 ]
In Python you can delete a range within a list by assigning an empty list to a slice.
>>> arr = ['red', 'orange', 'yellow', 'cyan', 'alpha'] >>> arr[1:4] =  >>> arr ['red', 'alpha']
You can also use the del keyword:
>>> arr = ['red', 'orange', 'yellow', 'cyan', 'alpha'] >>> del arr[1:4] >>> arr ['red', 'alpha']
Clearing Arrays and Lists
The most succinct way to empty out a JS array is:
arr.length = 0
But you can also use the splice method:
This replaces a section of the array beginning at the beginning, ending at the end, with nothing, since no additional items are provided.
You can do something equivalent in Python as well, using its splice syntax:
arr[:] = 
In most of these cases, adding a comment would probably be helpful, as it’s not immediately clear that emptying out the array is the intent.
The Step Parameter
The step parameter has an additional use, in that, when it’s negative, the slice goes in reverse.
When using the step parameter for splicing, there are some instances where it won’t work, like replacing one slice with a slice of a different size (including an empty one):
>> arr = list(range(10)) >>> arr [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> arr[1::2] =  Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: attempt to assign sequence of size 0 to extended slice of size 5
But you can use del to remove such a slice, even when the step parameter is used:
>> arr [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> del arr[1::2] >>> arr [0, 2, 4, 6, 8]
Negative indices used in the start and stop parameter are essentially aliases for the positive indices. For example, if the index is -1, it means the item at length – 1.
>>> arr [1, 2, 3, 4, 5] >>> arr[2:4] [3, 4] >>> arr[-3:-1] [3, 4]
Beware that using a negative step parameter means it will start *after* the index specified by the start parameter and stop *before* the index specified by the stop parameter.
>>> arr [1, 2, 3, 4, 5] >>> arr[-1:-3:-1] [5, 4]
For splicing in Python, negative indices work the same as with slicing.
>> arr Array(5) [ 1, 2, 3, 4, 5 ] >> arr.slice(2, 4) Array [ 3, 4 ] >> arr.slice(-3, -1) Array [ 3, 4 ]
>> arr Array(5) [ 1, 2, 3, 4, 5 ] >> arr.splice(-1, -1, 4.5) Array  >> arr Array(6) [ 1, 2, 3, 4, 4.5, 5 ] >> arr.splice(-1, 0, 4.75) Array  >> arr Array(7) [ 1, 2, 3, 4, 4.5, 4.75, 5 ]