Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s> Active reading [<http://en.wikipedia.org/wiki/Python_%28programming_language%29>].].
Source Link
Peter Mortensen

1. Slice Notation

1. Slice Notation

# mostlyMostly used variations
s[start:end]
s[start:]
s[:end]

# step Step-related variations
s[:end:step]
s[start::step]
s[::step]

# makeMake a copy
s[:]

NOTE: If start>=endstart >= end  (considering only when step>0), pythonPython will return a empty slice [].

2. Pitfalls

2. Pitfalls

The above part explains the core features on how slice works, and it will work on most occasions. However, there can be pitfalls you should watch out, and this part explains them.

Negative indexes

Negative indexes

The very first thing that confuses pythonPython learners is that indexan index can be negative! Don't panic: negativea negative index means count from backwards.

s[-5:]    # startStart at the 5th index from the end of array, 
          # thus returnsreturning the last 5 elements.
s[:-5]    # startStart at index 0, and end until the 5th index from end of array, 
          # thus returnsreturning s[0:len(s)-5].

Negative step

Negative step

MakeMaking things more confusing is that step can be negative too!

NegativeA negative step means iterate the array backwards: from the end to start, with the end index included, and the start index excluded from the result.

NOTE: when step is negative, the default value for start tois len(s)  (while end does not equal to 0, because s[::-1] contains s[0]). For example:

s[::-1]            # reversedReversed slice
s[len(s)::-1]      # The same as above, reversed slice
s[0:len(s):-1]     # emptyEmpty list

Out of range error?

Out of range error?

Be surprised: slice does not raise an IndexError when the index is out of range!

If the index is out of range, pythonPython will try its best to set the index to 0 or len(s) according to the situation. For example:

s[:len(s)+5]      # The same as s[:len(s)]
s[-len(s)-5::]    # The same as s[0:]
s[len(s)+5::-1]   # The same as s[len(s)::-1], and the same as s[::-1]

3. Examples

3. Examples

Let's finish this answer with examples explains, explaining everything we have discussed:

# createCreate our array for demonstration
In [1]: s = [i for i in range(10)]

In [2]: s
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: s[2:]   # fromFrom index 2 to last index
Out[3]: [2, 3, 4, 5, 6, 7, 8, 9]

In [4]: s[:8]   # fromFrom index 0 up to index 8
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7]

In [5]: s[4:7]  # fromFrom index 4 (included) up to index 7(excluded)
Out[5]: [4, 5, 6]

In [6]: s[:-2]  # upUp to second last index (negative index)
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7]

In [7]: s[-2:]  # fromFrom second last index (negative index)
Out[7]: [8, 9]

In [8]: s[::-1] # fromFrom last to first in reverse order (negative step)
Out[8]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [9]: s[::-2] # allAll odd numbers in reversed order
Out[9]: [9, 7, 5, 3, 1]

In [11]: s[-2::-2] # allAll even numbers in reversed order
Out[11]: [8, 6, 4, 2, 0]

In [12]: s[3:15]   # endEnd is out of range, pythonand Python will set it to len(s).
Out[12]: [3, 4, 5, 6, 7, 8, 9]

In [14]: s[5:1]    # startStart > end,end; return empty list
Out[14]: []

In [15]: s[11]     # accessAccess index 11 (greater than len(s)) will raise an IndexError
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-79ffc22473a3> in <module>()
----> 1 s[11]

IndexError: list index out of range

1. Slice Notation

# mostly used variations
s[start:end]
s[start:]
s[:end]

# step related variations
s[:end:step]
s[start::step]
s[::step]

# make a copy
s[:]

NOTE: If start>=end(considering only when step>0), python will return a empty slice [].

2. Pitfalls

The above part explains the core features on how slice works, it will work on most occasions. However there can be pitfalls you should watch out, and this part explains them.

Negative indexes

The very first thing confuses python learners is that index can be negative! Don't panic: negative index means count from backwards.

s[-5:]    # start at the 5th index from the end of array, 
          # thus returns the last 5 elements
s[:-5]    # start at index 0, end until the 5th index from end of array, 
          # thus returns s[0:len(s)-5]

Negative step

Make things more confusing is that step can be negative too!

Negative step means iterate the array backwards: from end to start, with end index included, and start index excluded from result.

NOTE: when step is negative, the default value for start to len(s)(while end does not equal to 0, because s[::-1] contains s[0]). For example:

s[::-1]            # reversed slice
s[len(s)::-1]      # same as above, reversed slice
s[0:len(s):-1]     # empty list

Out of range error?

Be surprised: slice does not raise IndexError when index is out of range!

If the index is out of range, python will try its best set the index to 0 or len(s) according to the situation. For example:

s[:len(s)+5]      # same as s[:len(s)]
s[-len(s)-5::]    # same as s[0:]
s[len(s)+5::-1]   # same as s[len(s)::-1], same as s[::-1]

3. Examples

Let's finish this answer with examples explains everything we have discussed:

# create our array for demonstration
In [1]: s = [i for i in range(10)]

In [2]: s
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: s[2:]   # from index 2 to last index
Out[3]: [2, 3, 4, 5, 6, 7, 8, 9]

In [4]: s[:8]   # from index 0 up to index 8
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7]

In [5]: s[4:7]  # from index 4(included) up to index 7(excluded)
Out[5]: [4, 5, 6]

In [6]: s[:-2]  # up to second last index(negative index)
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7]

In [7]: s[-2:]  # from second last index(negative index)
Out[7]: [8, 9]

In [8]: s[::-1] # from last to first in reverse order(negative step)
Out[8]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [9]: s[::-2] # all odd numbers in reversed order
Out[9]: [9, 7, 5, 3, 1]

In [11]: s[-2::-2] # all even numbers in reversed order
Out[11]: [8, 6, 4, 2, 0]

In [12]: s[3:15]   # end is out of range, python will set it to len(s)
Out[12]: [3, 4, 5, 6, 7, 8, 9]

In [14]: s[5:1]    # start > end, return empty list
Out[14]: []

In [15]: s[11]     # access index 11(greater than len(s)) will raise IndexError
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-79ffc22473a3> in <module>()
----> 1 s[11]

IndexError: list index out of range

1. Slice Notation

# Mostly used variations
s[start:end]
s[start:]
s[:end]

# Step-related variations
s[:end:step]
s[start::step]
s[::step]

# Make a copy
s[:]

NOTE: If start >= end  (considering only when step>0), Python will return a empty slice [].

2. Pitfalls

The above part explains the core features on how slice works, and it will work on most occasions. However, there can be pitfalls you should watch out, and this part explains them.

Negative indexes

The very first thing that confuses Python learners is that an index can be negative! Don't panic: a negative index means count backwards.

s[-5:]    # Start at the 5th index from the end of array,
          # thus returning the last 5 elements.
s[:-5]    # Start at index 0, and end until the 5th index from end of array,
          # thus returning s[0:len(s)-5].

Negative step

Making things more confusing is that step can be negative too!

A negative step means iterate the array backwards: from the end to start, with the end index included, and the start index excluded from the result.

NOTE: when step is negative, the default value for start is len(s)  (while end does not equal to 0, because s[::-1] contains s[0]). For example:

s[::-1]            # Reversed slice
s[len(s)::-1]      # The same as above, reversed slice
s[0:len(s):-1]     # Empty list

Out of range error?

Be surprised: slice does not raise an IndexError when the index is out of range!

If the index is out of range, Python will try its best to set the index to 0 or len(s) according to the situation. For example:

s[:len(s)+5]      # The same as s[:len(s)]
s[-len(s)-5::]    # The same as s[0:]
s[len(s)+5::-1]   # The same as s[len(s)::-1], and the same as s[::-1]

3. Examples

Let's finish this answer with examples, explaining everything we have discussed:

# Create our array for demonstration
In [1]: s = [i for i in range(10)]

In [2]: s
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: s[2:]   # From index 2 to last index
Out[3]: [2, 3, 4, 5, 6, 7, 8, 9]

In [4]: s[:8]   # From index 0 up to index 8
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7]

In [5]: s[4:7]  # From index 4 (included) up to index 7(excluded)
Out[5]: [4, 5, 6]

In [6]: s[:-2]  # Up to second last index (negative index)
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7]

In [7]: s[-2:]  # From second last index (negative index)
Out[7]: [8, 9]

In [8]: s[::-1] # From last to first in reverse order (negative step)
Out[8]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [9]: s[::-2] # All odd numbers in reversed order
Out[9]: [9, 7, 5, 3, 1]

In [11]: s[-2::-2] # All even numbers in reversed order
Out[11]: [8, 6, 4, 2, 0]

In [12]: s[3:15]   # End is out of range, and Python will set it to len(s).
Out[12]: [3, 4, 5, 6, 7, 8, 9]

In [14]: s[5:1]    # Start > end; return empty list
Out[14]: []

In [15]: s[11]     # Access index 11 (greater than len(s)) will raise an IndexError
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-79ffc22473a3> in <module>()
----> 1 s[11]

IndexError: list index out of range
add space to comment
Source Link
pylang
s[::-1]            # reversed slice
s[len(s)::-1]      # same as above, reversed slice
s[0:len(s):-1]     # empty list
s[::-1]            # reversed slice
s[len(s)::-1]     # same as above, reversed slice
s[0:len(s):-1]     # empty list
s[::-1]            # reversed slice
s[len(s)::-1]      # same as above, reversed slice
s[0:len(s):-1]     # empty list
fixed a logic bug.
Source Link
cizixs

NOTENOTE: when step is negative, the default value for start and end changes to len(s) and(while end does not equal to 0 respectively, because s[::-1] contains s[0]). For example:

s[::-1]            # reversed slice
s[len(s):0:-1]     # same as above, reversed slice
s[0:len(s):-1]     # empty list

NOTE: when step is negative, the default value for start and end changes to len(s) and 0 respectively. For example:

s[::-1]            # reversed slice
s[len(s):0:-1]     # same as above, reversed slice
s[0:len(s):-1]     # empty list

NOTE: when step is negative, the default value for start to len(s)(while end does not equal to 0, because s[::-1] contains s[0]). For example:

s[::-1]            # reversed slice
s[len(s)::-1]     # same as above, reversed slice
s[0:len(s):-1]     # empty list
Source Link
cizixs
Loading
lang-py