Codectionary / Developer documentation / Python

Slicing

Slicing extracts a sub-portion of a sequence (list, tuple, or string) using the [start:stop:step] syntax. The slice includes the start index but excludes the stop index, similar to range(). All three parts are optional, and negative indices count from the end of the sequence, making slicing a compact way to grab sub-ranges, reverse sequences, or skip elements.

Syntax

sequence[start:stop:step]

Examples

Basic Slicing

Extracting a portion of a list or string by index range.

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(numbers[2:5])   # [2, 3, 4]  (index 5 is excluded)
print(numbers[:4])    # [0, 1, 2, 3]  (from the start)
print(numbers[6:])    # [6, 7, 8, 9]  (to the end)
print(numbers[:])     # a full copy of the list

Negative Indices

Counting from the end of a sequence.

text = "Codectionary"

print(text[-1])     # y (last character)
print(text[-4:])    # nary (last four characters)
print(text[:-4])    # Codectio (everything except the last four)

Step Slicing

Skipping elements using the step parameter.

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(numbers[::2])   # [0, 2, 4, 6, 8]  (every second item)
print(numbers[1::2])  # [1, 3, 5, 7, 9]  (odd indices)

Reversing with Slicing

A common idiom: [::-1] reverses any sequence.

text = "Python"
print(text[::-1])  # nohtyP

numbers = [1, 2, 3, 4, 5]
print(numbers[::-1])  # [5, 4, 3, 2, 1]

Best practices

  • Remember the stop index in a slice is exclusive - sequence[2:5] gives 3 items (indices 2, 3, 4), not 4
  • Use sequence[:] to make a shallow copy of a list, a common and idiomatic pattern
  • Use [::-1] for a quick, readable way to reverse a string, list, or tuple
  • Slicing never raises an IndexError even with out-of-range indices - it just returns as much as is available, unlike direct indexing

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

Lists
A list is Python's built-in ordered, mutable collection type, created with square brackets. Lists can hold items of any type - even a mix of types - and support indexing, slicing, and a wide range of built-in methods for adding, removing, and reordering elements. Because they are mutable, lists can be changed in place after creation, which makes them the go-to structure for collections that grow or shrink over time.
Tuples
A tuple is an ordered, immutable collection, created with parentheses (or often just commas). Once created, a tuple's contents cannot be changed, added to, or removed - this immutability makes tuples faster than lists and safe to use as dictionary keys or in sets. Tuples are commonly used for fixed collections of related values, like coordinates or RGB colors, and for returning multiple values from a function.
Dictionaries
A dictionary stores data as key-value pairs, created with curly braces. Keys must be unique and hashable (strings, numbers, or tuples are common choices), while values can be anything, including other dictionaries or lists. Since Python 3.7, dictionaries maintain insertion order. They are one of the most heavily used data structures in Python, ideal for representing structured records, lookups, and mappings.
Sets
A set is an unordered collection of unique, hashable items, created with curly braces or the set() function. Sets automatically eliminate duplicates and support fast membership testing, along with mathematical set operations like union, intersection, and difference. They're especially useful for deduplicating data and for comparing two collections to find overlaps or differences.