Slicing with Negative Numbers in Python
Last Updated : 18 Sep, 2025
In Python, slicing allows us to extract parts of sequences like strings, lists and tuples. While normal slicing uses positive indices, Python also supports negative slicing, which makes it easier to work with elements starting from the end of a sequence.
Slicing with Negative NumbersNegative Indexing
Python uses negative indices to count elements from the end.
- -1 refers to the last element
- -2 refers to the second-last element
- -3 refers to the third-last element and so on.
This feature is especially useful when you don’t know exact length of a sequence but still want to access elements from the end.
Example: Here we access elements from the end of a string using negative indices.
Python text = "Python" print(text[-1]) print(text[-3])
Explanation:
- text[-1] -> returns "n", last character.
- text[-3] -> returns "h", third-last character.
Ways to Perform Negative Slicing
In Python, negative slicing can be done in two ways:
- Using colon (:) operator
- Using slice() function
Both methods allow us to specify the start, end and step positions using negative indices.
1. Using Colon (:) Operator
This is the most common way to perform slicing. The colon (:) operator accepts three optional parameters:
Syntax:
sequence[start : end : step]
Parameters:
- start: Starting index of the slice (can be negative or positive).
- end: Ending index of the slice (exclusive, not included).
- step: Number of steps to move (use -1 to reverse).
Example: Here we slice a string into left, middle and right parts using negative indices.
Python text = "GeeksforGeeks" # Slicing left part of string text left = text[:-8] # Slicing middle part of string text middle = text[-8:-5] # Slicing right part of string text right = text[-5:] print(left) print(middle) print(right)
Explanation:
- text[:-8] -> Slices from start to index -8, giving "Geeks".
- text[-8:-5] -> Slices characters between -8 and -5, giving "for".
- text[-5:] -> Slices from -5 till the end, giving "Geeks".
2. Using slice() Function
Python also provides the slice() function to achieve the same result in a more explicit way.
Syntax:
slice(start, stop, step)
Parameters:
- start: Starting index (can be None or negative).
- stop: Ending index (exclusive).
- step: Step value (negative step reverses the sequence).
Example: Here we use the slice() function to achieve the same slicing as before.
Python text = "GeeksforGeeks" # Slicing left part of string text left = text[slice(-8)] # Slicing middle part of string text middle = text[slice(-8, -5)] # Slicing right part of string text right = text[slice(-5, None)] print(left) print(middle) print(right)
Explanation:
- slice(-8) -> Slices from start till index -8.
- slice(-8, -5) -> Slices from -8 to -5.
- slice(-5, None) -> Slices from -5 till the end.
More Examples
Negative slicing allows us to work with elements from the end of sequences like lists and tuples. Below are some useful examples.
1. Slicing List with Negative Numbers
Example: In this code, we create a list called items. Using negative slicing, we extract last four elements as vegetables and two elements before them as fruits.
Python li = ["pen", "pencil", "eraser", "apple", "guava", "ginger","Potato", "carrot", "Chilli"] a = li[-4:] print(a) b = li[slice(-6, -4)] print(b)
Output['ginger', 'Potato', 'carrot', 'Chilli'] ['apple', 'guava']
2. Reverse List with the Negative Step
Example: Here, we reverse entire list by passing -1 as the step value. The first method uses normal slicing and second uses slice() function with None for start and end and -1 as step.
Python num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] rev = num[::-1] print(rev) rev2 = num[slice(None, None, -1)] print(rev2)
Output[10, 9, 8, 7, 6, 5, 4, 3, 2, 1] [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
3. Slicing Alternates in the List with Negative Step
Example: In this example, we extract alternate elements from the list in reverse order. By passing -2 as the step, we skip one element while moving backwards.
Python num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] alt = num[::-2] print(alt) alt2 = num[slice(None, None, -2)] print(alt2)
Output[10, 8, 6, 4, 2] [10, 8, 6, 4, 2]
4. Slicing Tuples with Negative Numbers
Example: This code demonstrates negative slicing with tuples. We access last three elements, select a middle range, reverse the tuple and also extract alternate elements in reverse.
Python tup = (1, 2, 3, 4, 5, 6, 7, 8, 9) s = tup[-3:] print(s) s = tup[-5:-2] print(s) rev_tup = tup[::-1] print(rev_tup) s = tup[::-2] print(s)
Output(7, 8, 9) (5, 6, 7) (9, 8, 7, 6, 5, 4, 3, 2, 1) (9, 7, 5, 3, 1)
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice