The syntax is:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relationship with the slice object

A slice object can represent a slicing operation, i.e.:

a[start:stop:step]

is equivalent to:

a[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similar to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

Answer from Greg Hewgill on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_slicing.asp
NumPy Array Slicing
We pass slice instead of index like this: [start:end]. We can also define the step, like this: [start:end:step]. ... Note: The result includes the start index, but excludes the end index.
Top answer
1 of 16
6665

The syntax is:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relationship with the slice object

A slice object can represent a slicing operation, i.e.:

a[start:stop:step]

is equivalent to:

a[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similar to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

2 of 16
729

The Python tutorial talks about it (scroll down a bit until you get to the part about slicing).

The ASCII art diagram is helpful too for remembering how slices work:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
   0   1   2   3   4   5
  -6  -5  -4  -3  -2  -1

One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n.

๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array-slicing
NumPy Array Slicing
Here, slice_arr contains elements [2, 3, 4], which are sliced from the original array arr starting at index 2 and ending before index 5.
๐ŸŒ
Problem Solving with Python
problemsolvingwithpython.com โ€บ 05-NumPy-and-Arrays โ€บ 05.06-Array-Slicing
Array Slicing - Problem Solving with Python
Therefore, the slicing operation [:2] pulls out the first and second values in an array. The slicing operation [1:] pull out the second through the last values in an array. The example below illustrates the default stop value is the last value in the array.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ array โ€บ array-slicing-in-python
Understanding Array Slicing in Python - AskPython
February 12, 2023 - As we can see for both the cases, start and step are set by default to 0 and 1. The sliced arrays contain elements of indices 0 to (stop-1). This is one of the quickest methods of array slicing in Python. Again, specifying any two parameters among the start, stop and end, you can perform array slicing in Python by considering default value for the third parameter. Let us take an example.
๐ŸŒ
Python Central
pythoncentral.io โ€บ how-to-slice-listsarrays-and-tuples-in-python
How to Slice Lists/Arrays and Tuples in Python | Python Central
July 18, 2022 - Getting the first โ€œNโ€ elements ... output the first three elements of the list by slicing: [python] items = ['car', 'bike', 'house', 'bank', 'purse', 'photo', 'box'] sub_items = items[:3] print(sub_items) [/python]...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-slicing-how-to-slice-an-array
Python Slicing โ€“ How to Slice an Array and What Does [::-1] Mean?
December 8, 2022 - By passing 2: in the square brackets, the slicing starts from index 2 (which holds value 3) up until the end of the array, as you can see in the results. For example, if you want to slice an array from the first value to the third, here's how:
๐ŸŒ
Pythonhealthdatascience
pythonhealthdatascience.com โ€บ content โ€บ 01_algorithms โ€บ 03_numpy โ€บ 03_slicing.html
Array slicing and indexing โ€” Python for health data science.
Originally we were restricting our slice to the 1st element i.e. k=0 of each array returned. If we want all elements we replace the 0 with : ... Getting to grips with multi-dimensional arrays takes time and practice. So do persevere. If you have only ever coded in python before and have no ...
Find elsewhere
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ numpy-array-slicing-in-python
NumPy Array Slicing in Python - StrataScratch
March 1, 2024 - Negative indices allow you to start counting from the end of the array. # Select the last three elements neg_slice = arr[-3:] print("Last three elements:", neg_slice)
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ the python coding stack โ€บ a slicing story
A Slicing Story - by Stephen Gruppetta
June 25, 2024 - I have used lists in all the examples in this article so far. You can slice other sequences, too: And this also creates a copy. When slicing strings or tuples, there's no other option other than creating a copy since they're immutable data types. So, let's try another data type to see whether slicing always creates a copy. Let's use a NumPy array...
๐ŸŒ
Nanyang Technological University
libguides.ntu.edu.sg โ€บ python โ€บ arrayslicing
NP.4 Array Slicing - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
2 weeks ago - With a (3,3) shaped array, we know ... row by replacing 0 with the proper index as long as they are within range. In this example, we are retrieving elements from the 2nd column....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_indexing_and_slicing.htm
NumPy - Indexing & Slicing
In the below code we will see how ... 1 to 12 and need to access only even numbers, we use slicing with step parameter 'arr[::2]' as it slices every second element in the array....
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-slicing
NumPy Array Slicing (With Examples)
In the above example, we have created the array named array1 with 9 elements. Then, we used the slicing operator : to slice array elements.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-slice
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - Versatility across data structures: Slicing works seamlessly across Python's ecosystem, from strings and lists to more advanced structures like NumPy arrays and pandas data frames. Real-world applications: Slicing simplifies everyday coding tasks. Need to process only a segment of a dataset? Extract specific columns from a table? Or reverse a string for a text analysis project? Slicing is your go-to. As I said, slicing is a core feature in Python, allowing developers to extract portions of sequences like lists, strings, and tuples. Python offers two primary ways to slice sequences without importing anything: the slicing : syntax and the slice() function.
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ guide-to-numpy-array-slicing
A Useful Guide to NumPy Array Slicing
The minus operator is used to refer to an index from the end of an array; you slice an array from the end instead of from the start. Example: Slice from index 4 (from the end) to index 2 (from the end).
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ python slice notation
Python slice notation | Sentry
October 21, 2022 - The value of the array at the stop index is not included in the slice. num_string = "012345" zero_to_four = num_string[0:5] print(zero_to_four) ... If an index is not provided for start, the slice will begin at the start of the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ slice-a-2d-array-in-python
Slice a 2D Array in Python - GeeksforGeeks
July 23, 2025 - Below are some of the ways by which we can slice a 2D array in Python: ... In this example, matrix[0:2] selects the first and second rows, and [1:3] extracts the second and third columns.
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ slice-python
https://www.dotnetperls.com/slice-python
Note Python elements begin at index 0. So the first index (for slices or indexes) is zero. Here We continue up to (but not including) index 3, which is the value 400. So our result list has 200 and 300 in it. ... The second index in slice notation may be negative. This means counting begins from the last index. So a negative one means the same as "length minus one." So You can reduce an array length by one by using a second index of negative one.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-slicing-multi-dimensional-arrays
Python Slicing Multi-Dimensional Arrays - GeeksforGeeks
July 1, 2026 - This produces a new 2-D array containing the last column values from every matrix. Slices can be used not only for reading data but also for updating multiple values at once.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ slice
Python slice()
Now we know about slice objects, let's see how we can get substring, sub-list, sub-tuple, etc. from slice objects. # Program to get a substring from the given string py_string = 'Python' # stop = 3 # contains 0, 1 and 2 indices