List slicing is a technique used to extract a portion of a list by specifying a range of indices. It returns a new list containing the selected elements while leaving the original list unchanged.
fruits = ["Apple", "Banana", "Orange", "Mango", "Grapes", "Kiwi"]
print(fruits[2:5])
Output
['Orange', 'Mango', 'Grapes']
Explanation: slice fruits[2:5] starts from index 2 ("Orange") and ends before index 5, so it returns ["Orange", "Mango", "Grapes"]. The original list remains unchanged.
Syntax
list_name[start : end : step]
Parameters:
- start (optional): Index to begin the slice (inclusive). Defaults to 0 if omitted.
- end (optional): Index to end the slice (exclusive). Defaults to the length of list if omitted.
- step (optional): Step size, specifying the interval between elements. Defaults to 1 if omitted
Examples
1. Get All Items: If you want to create a copy of the entire list or access all its elements, you can use slicing without specifying any indices.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[:])
print(a[::])
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation: Both [:] and [::] return a copy of the complete list because no start, stop, or step values are specified.
2. Get Items Before or After a Specific Position: You can retrieve all elements before a given index or all elements from a given index to the end of the list.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a[2:]
c = a[:3]
print(b)
print(c)
Output
[3, 4, 5, 6, 7, 8, 9] [1, 2, 3]
Explanation:
- a[2:] returns all elements starting from index 2.
- a[:3] returns all elements before index 3 (excluding index 3).
3. Get Items Between Two Positions: To extract a specific portion of a list, provide both the starting and ending indices.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a[1:4]
print(b)
Output
[2, 3, 4]
Explanation: slice starts at index 1 and stops before index 4, returning the elements at indices 1, 2, and 3.
4. Get Items at Specific Intervals: step parameter allows you to skip elements while slicing.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a[::2]
c = a[1:8:3]
print(b)
print(c)
Output
[1, 3, 5, 7, 9] [2, 5, 8]
Explanation:
- a[::2] returns every second element from the entire list.
- a[1:8:3] starts at index 1, stops before index 8, and selects every third element.
5. Out-of-Bound Slicing: Unlike indexing, list slicing does not raise an error if the specified indices are outside the list's range.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[7:15])
print(a[15:20])
Output
[8, 9] []
Explanation:
- a[7:15] returns the available elements from index 7 to the end of the list.
- a[15:20] returns an empty list because the starting index is beyond the list length.
Negative Indexing
Negative indexing allows us to access elements from the end of a list. The last element has an index of -1, the second last -2, and so on. It is useful when you want to work with the last few elements without calculating the length of the list.
Example 1: In this example, we use negative indices to retrieve elements from different positions starting from the end of the list.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[-2:])
print(a[:-3])
print(a[-4:-1])
print(a[-8:-1:2])
Output
[8, 9] [1, 2, 3, 4, 5, 6] [6, 7, 8] [2, 4, 6, 8]
Explanation:
- a[-2:] returns the last two elements of the list.
- a[:-3] returns all elements except the last three.
- a[-4:-1] returns elements from the fourth-last element up to (but not including) the last element.
- a[-8:-1:2] returns every second element within the specified negative index range.
Example 2: In this example, we use a negative step (-1) to traverse the list in reverse order and create a reversed copy.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a[::-1]
print(b)
Output
[9, 8, 7, 6, 5, 4, 3, 2, 1]
Explanation: slice a[::-1] starts from the end of the list and moves backward one element at a time, returning a reversed copy of the list. The original list remains unchanged.