Advance Python - V

Slicing (List, Tuple, and String)

Slicing in Python is a technique used to extract specific portions of sequences like lists, tuples, or strings. By slicing, you can create new sequences containing selected elements from the original sequence.

Slicing Syntax

The general syntax for slicing is:

sequence[start : stop : step]
  • start – Index where the slice begins (inclusive).

  • stop – Index where the slice ends (exclusive).

  • step – Interval between indices (optional).

Indexing in Sequences

Python sequences use zero-based indexing. The first element has index 0. Negative indices access elements from the end of the sequence, e.g. -1 is the last element, -2 the second last, etc.

Examples of Slicing

# Initialize list

numbers = [5, 10, 15, 20, 25, 30, 35]

# 1. Full slice
print("Full slice:", numbers[:])

# 2. Slice from index 3 to end
print("Slice from index 3:", numbers[3:])

# 3. Slice from start to index 4 (exclusive)
print("Slice to index 4:", numbers[:4])

# 4. Slice with step of 2
print("Slice with step=2:", numbers[::2])

# 5. Reverse the list
print("Reverse list:", numbers[::-1])

Output:

Full slice: [5, 10, 15, 20, 25, 30, 35]
Slice from index 3: [20, 25, 30, 35]
Slice to index 4: [5, 10, 15, 20]
Slice with step=2: [5, 15, 25, 35]
Reverse list: [35, 30, 25, 20, 15, 10, 5]

Example 2 – More Slicing Variations

# Initialize list
fruits = ["apple", "banana", "cherry", "date", "fig", "grape"]

# 6. Slice from index 2, every 2nd element
print("Start at index 2, step 2:", fruits[2::2])

# 7. Slice from start to index 5, every 3rd element
print("Slice to index 5, step 3:", fruits[:5:3])

# 8. Slice using negative indices
print("Negative indices (-4:-1):", fruits[-4:-1])

# 9. Reverse a string
text = "HelloWorld"
print("Reversed string:", text[::-1])

# 10. Slice a tuple with step
numbers_tuple = (2, 4, 6, 8, 10, 12, 14)
print("Tuple slice step=2:", numbers_tuple[::2])

Output:

Start at index 2, step 2: ['cherry', 'fig']
Slice to index 5, step 3: ['apple', 'date']
Negative indices (-4:-1): ['cherry', 'date', 'fig']
Reversed string: dlroWolleH
Tuple slice step=2: (2, 6, 10, 14)

Slicing in Lists

List slicing works seamlessly with positive and negative indices. Example:

colors = ["red", "green", "blue", "yellow", "purple"]

# Slice from index 1 to 4
print(colors[1:4])

Output:

['green', 'blue', 'yellow']

Slicing in Tuples

Tuple slicing uses the same syntax as lists:

tup = (100, 200, 300, 400, 500, 600)

# Slice from index 2 to 5
print(tup[2:5])

Output:

(300, 400, 500)

Slicing in Strings

Strings support slicing just like lists and tuples:

text = "DataScience"

# Slice from index 2 to 7
print(text[2:7])

Output:

taSci

Slicing Differences Between List, Tuple, and String

👀Here’s an interesting difference:

list_a = [1, 2, 3]
list_b = list_a[:]

tuple_a = (1, 2, 3)
tuple_b = tuple_a[:]

string_a = "abc"
string_b = string_a[:]

print(list_a is list_b)
print(tuple_a is tuple_b)
print(string_a is string_b)

Output:

False
True
True
  • When you slice a list, a new list object is created, so list_a and list_b are different objects.

  • For tuples and strings (which are immutable), slicing often returns the same object if the slice represents the whole sequence.

Popular Posts