Skip to content

Sequence Slicing

TypeScript gives you .slice() — Python gives you [start:stop:step]

Section titled “TypeScript gives you .slice() — Python gives you [start:stop:step]”

TypeScript arrays have Array.prototype.slice(start, end), which takes two arguments and returns a copy of the subarray. It is useful, but limited: no step parameter, and negative indices only work in a specific way (counting from the end). Strings have String.prototype.slice() with the same interface.

Python’s slice syntax [start:stop:step] is built into the language itself and works on any sequence type — lists, strings, tuples, bytes, and any class that implements __getitem__. The step parameter lets you skip elements or reverse a sequence in one expression. Negative indices count from the end, and any of the three parameters can be omitted.

TypeScript
// TypeScript: Array.slice(start, end)
const data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(data.slice(2, 5)); // [2, 3, 4]
console.log(data.slice(0, 3)); // [0, 1, 2]
console.log(data.slice(7)); // [7, 8, 9]
// No built-in step — must use filter or manual loop
const evens = data.filter((_, i) => i % 2 === 0);
console.log(evens); // [0, 2, 4, 6, 8]
// Reverse — no shorthand
const reversed = [...data].reverse();
console.log(reversed);
// Negative index from end
console.log(data[data.length - 1]); // 9
console.log(data.slice(-3)); // [7, 8, 9]
Python
# Python: [start:stop:step] — works on any sequence
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[2:5]) # [2, 3, 4]
print(data[:3]) # [0, 1, 2]
print(data[7:]) # [7, 8, 9]
print(data[::2]) # [0, 2, 4, 6, 8] (every 2nd element)
print(data[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (reversed)
# Negative indices count from the end
print(data[-1]) # 9
print(data[-3:]) # [7, 8, 9]
print(data[1:-1]) # [1, 2, 3, 4, 5, 6, 7, 8]

TypeScript requires .at() (ES2022) or manual array.length - n math for negative indexing. Python supports negative indices natively on all sequences.

TypeScript
// TypeScript: negative index requires .at() (ES2022) or length math
const data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(data.at(-1)); // 9 — last element
console.log(data.at(-3)); // 7
// Last 3 elements via slice
console.log(data.slice(-3)); // [7, 8, 9]
// All but first and last
console.log(data.slice(1, -1)); // [1, 2, 3, 4, 5, 6, 7, 8]
Python
# Python: negative index works natively everywhere
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[-1]) # 9 — last element
print(data[-3:]) # [7, 8, 9] — last 3 elements
print(data[1:-1]) # [1, 2, 3, 4, 5, 6, 7, 8] — all but first and last

The same [start:stop:step] syntax works on strings, tuples, and any sequence — not just lists.

TypeScript
// TypeScript: string slicing
const text = "Hello, World!";
console.log(text.slice(7, 12)); // "World"
console.log(text.slice(0, 5)); // "Hello"
// Reverse a string — no shorthand
const reversed = text.split("").reverse().join("");
console.log(reversed); // "!dlroW ,olleH"
Python
# Python: slicing works on strings too
text = "Hello, World!"
print(text[7:12]) # World
print(text[:5]) # Hello
print(text[::-1]) # !dlroW ,olleH (reversed in one expression)
# Works on tuples too
coords = (10, 20, 30, 40, 50)
print(coords[1:4]) # (20, 30, 40)
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[2:5])
print(data[:3])
print(data[7:])
print(data[::2])
print(data[::-1])
print(data[-1])
print(data[-3:])
print(data[1:-1])
text = "Hello, World!"
print(text[7:12])
print(text[::-1])
What does data[::2] return for data = [0, 1, 2, 3, 4]?
What is the idiomatic Python way to reverse a list using slicing?
What does slicing return in terms of memory?
What is slice(1, 5, 2) equivalent to as a slice expression?