0% found this document useful (0 votes)
11 views3 pages

Slicing Tuple and List

The document provides a comprehensive guide on tuple and list slicing in Python, detailing various methods such as basic slicing, negative indices, and slicing with steps. It includes multiple examples for each method, illustrating how to extract elements from tuples and lists. Key points include the ability to reverse sequences and the behavior of empty slices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Slicing Tuple and List

The document provides a comprehensive guide on tuple and list slicing in Python, detailing various methods such as basic slicing, negative indices, and slicing with steps. It includes multiple examples for each method, illustrating how to extract elements from tuples and lists. Key points include the ability to reverse sequences and the behavior of empty slices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tuple Slicing

Example 1: Basic Slicing (Get elements from index 1 to 3)


tup = (0, 1, 2, 3, 4, 5)
sliced_tup = tup[1:4]
print(sliced_tup)
Output: (1, 2, 3)
Explanation: The slice [1:4] extracts elements starting from index 1 up to (but not including) index 4.

Example 2: Slicing with Negative Indices


tup = (10, 20, 30, 40, 50)
sliced_tup = tup[-4:-1]
print(sliced_tup)
Output: (20, 30, 40)
Explanation: The slice [-4:-1] starts from the 4th element from the end and goes up to (but does not include) the
1st element from the end.

Example 3: Slicing with Step (Every second element)


tup = (0, 1, 2, 3, 4, 5)
sliced_tup = tup[::2]
print(sliced_tup)
Output: (0, 2, 4)
Explanation: The slice [::2] means "start from the beginning and take every second element."

Example 4: Slicing with Negative Step (Reverse Order)


tup = (1, 2, 3, 4, 5)
sliced_tup = tup[::-1]
print(sliced_tup)
Output: (5, 4, 3, 2, 1)
Explanation: The slice [::-1] reverses the tuple.

Example 5: Slicing with Start, Stop, and Step


tup = (0, 1, 2, 3, 4, 5, 6)
sliced_tup = tup[1:6:2]
print(sliced_tup)
Output: (1, 3, 5)
Explanation: The slice [1:6:2] starts at index 1, goes up to index 6 (but does not include 6), and takes every
second element.

Example 6: Slicing the First Three Elements


tup = (1, 2, 3, 4, 5)
sliced_tup = tup[:3]
print(sliced_tup)
Output: (1, 2, 3)
Explanation: The slice [:3] extracts elements from the beginning of the tuple up to (but not including) index 3.

Example 7: Slicing the Last Three Elements


tup = (10, 20, 30, 40, 50)
sliced_tup = tup[-3:]
print(sliced_tup)
Output: (30, 40, 50)
Explanation: The slice [-3:] extracts the last three elements of the tuple.

Example 8: Slicing with Start at Index 2 and No Stop


tup = (10, 20, 30, 40, 50)
sliced_tup = tup[2:]
print(sliced_tup)
Output: (30, 40, 50)
Explanation: The slice [2:] starts at index 2 and goes until the end of the tuple.

Example 9: Empty Tuple Slice


tup = (1, 2, 3, 4, 5)
sliced_tup = tup[3:3]
print(sliced_tup)
Output: ()
Explanation: The slice [3:3] results in an empty tuple because the starting index is equal to the ending index, so
no elements are included.

Example 10: Slicing with Step of -2


tup = (10, 20, 30, 40, 50, 60)
sliced_tup = tup[5:0:-2]
print(sliced_tup)
Output: (60, 40, 20)
Explanation: The slice [5:0:-2] starts from index 5 (element 60), goes backward in steps of 2, and ends just before
index 0 (but does not include 0).

Summary of Tuple Slicing:


1. Basic Slicing: tup[start:end] — Extracts elements from index start to end-1.
2. Negative Indices: tup[-n] — Start counting from the end of the tuple.
3. Step: tup[start:end:step] — Specifies step size to skip elements.
4. Reverse: tup[::-1] — Reverses the tuple.
List Slicing
Example 1: Basic Slicing (Get elements from index 1 to 3)
lst = [10, 20, 30, 40, 50]
sliced_lst = lst[1:4]
print(sliced_lst)
Output: [20, 30, 40]
Explanation: The slice [1:4] extracts elements starting from index 1 up to (but not including) index 4.

Example 2: Slicing with Negative Indices


lst = [100, 200, 300, 400, 500]
sliced_lst = lst[-4:-1]
print(sliced_lst)
Output: [200, 300, 400]
Explanation: The slice [-4:-1] starts from the 4th element from the end and goes up to (but does not include) the
1st element from the end.

Example 3: Slicing with Step (Every second element)


lst = [0, 1, 2, 3, 4, 5]
sliced_lst = lst[::2]
print(sliced_lst)
Output: [0, 2, 4]
Explanation: The slice [::2] means "start from the beginning and take every second element."

Example 4: Slicing with Negative Step (Reverse Order)


lst = [1, 2, 3, 4, 5]
sliced_lst = lst[::-1]
print(sliced_lst)
Output: [5, 4, 3, 2, 1]
Explanation: The slice [::-1] reverses the list.

Example 5: Slicing with Start, Stop, and Step


lst = [10, 20, 30, 40, 50, 60, 70]
sliced_lst = lst[1:6:2]
print(sliced_lst)
Output: [20, 40, 60]
Explanation: The slice [1:6:2] starts at index 1, goes up to index 6 (but does not include index 6), and takes every
second element.

Example 6: Slicing the First Three Elements


lst = [1, 2, 3, 4, 5]
sliced_lst = lst[:3]
print(sliced_lst)
Output: [1, 2, 3]
Explanation: The slice [:3] extracts elements from the beginning of the list up to (but not including) index 3.

Example 7: Slicing the Last Three Elements


lst = [10, 20, 30, 40, 50]
sliced_lst = lst[-3:]
print(sliced_lst)
Output: [30, 40, 50]
Explanation: The slice [-3:] extracts the last three elements of the list.

Example 8: Slicing with Start at Index 2 and No Stop


lst = [10, 20, 30, 40, 50]
sliced_lst = lst[2:]
print(sliced_lst)
Output: [30, 40, 50]
Explanation: The slice [2:] starts at index 2 and goes until the end of the list.

Example 9: Empty List Slice


lst = [1, 2, 3, 4, 5]
sliced_lst = lst[3:3]
print(sliced_lst)
Output: []
Explanation: The slice [3:3] results in an empty list because the starting index is equal to the ending index, so no
elements are included.

Example 10: Slicing with Step of -2 (Reverse with Skipping)


lst = [10, 20, 30, 40, 50, 60]
sliced_lst = lst[5:0:-2]
print(sliced_lst)
Output: [60, 40, 20]
Explanation: The slice [5:0:-2] starts from index 5 (element 60), goes backward in steps of 2, and ends just before
index 0 (but does not include 0).

Summary of List Slicing:


1. Basic Slicing: lst[start:end] — Extracts elements from index start to end-1.
2. Negative Indices: lst[-n] — Start counting from the end of the list.
3. Step: lst[start:end:step] — Specifies step size to skip elements.
4. Reverse: lst[::-1] — Reverses the list.
5. Empty Slice: lst[start:end] where start == end returns an empty list.
Let me know if you need more Examples or further clarification!

You might also like