Sequences and Strings
-
Zero-Based Indexing
A zero-based index counts how many steps an element is from the start of a sequence.
-
Negative Indices in Python Sequences
A negative index identifies a position relative to the sequence's end when the sequence supports that convention.
-
Half-Open Slices of Sequences
A slice with step one usually includes its start offset and excludes its stop offset. Its bounds describe the spaces between successive elements.
-
Steps and Direction in a Slice
A slice step controls the spacing and direction of selected positions. Negative steps visit the sequence from right to left.
-
Slice Bounds and Single-Index Bounds
Python's built-in sequences treat excessive slice endpoints and an excessive single index differently.
-
Appending, Inserting, and Extending a List
Appending adds one item, inserting puts an item at a specified position, and extending draws items from an iterable.
-
Removing a List Element by Value or Position
The value-based removal of a list element finds a matching value. Position-based removal identifies which occurrence by index.
-
Copying a Sequence with a Slice
A full slice of a Python list creates another outer list. Nested mutable values can still be shared.
-
Sequence Repetition and Shared Elements
Repetition can duplicate references in a sequence. It may leave repeated positions sharing a single mutable object.
-
Sequences for Stacks and Queues
Choose the operations your sequence needs most often, since front and back operations can have different costs.
-
Finding a Value in an Ordered Sequence
A sequence can be searched in visitation order. A sorted sequence also gives information from each comparison that can eliminate positions.
-
Stable Ordering of Equal-Key Items
A stable sort keeps the relative order of inputs whose sort keys compare equal.
-
Sorting With a Key Function
A key function maps each item to the value the sort should use in comparisons. Keep it consistent with what the result is meant to show.
-
Sorting in Place or Producing a Sorted Copy
Changing a list in place exposes the order to existing references. A sorted result returned as a separate list can preserve source order.
-
Rows and Columns in Nested Lists
A nested list commonly uses one index to choose a row and another to choose a value inside it.
-
Flattening a Rectangular Grid by Rows
A rectangular grid stored row by row can translate coordinates into one-dimensional offsets if its width is fixed.
-
Finding a Delimiter or Splitting Text
Finding a delimiter yields a position or a missing marker. Splitting uses rules that decide how many fields to produce.
-
Stripping Text and Removing an Exact Prefix
Stripping removes characters from the ends according to a character set. Removing a prefix checks a single complete leading substring.
-
Joining a Sequence of Text Fragments
A separator-based join places a delimiter between neighboring fragments and gives an exact layout for empty and singleton inputs.
-
Substrings and Subsequences
A substring occupies contiguous positions. A subsequence preserves relative order while allowing gaps.
-
Removing Duplicates While Preserving Input Order
An order-preserving deduplication walks the input and emits a value only at the first key occurrence.
-
Counting Consecutive Runs of Symbols
A run consists of adjacent equal symbols. Tracking its length can summarize a sequence while preserving changes in order.
-
Code Points and Visible Characters in Strings
A text position may count bytes, Unicode code points, or grapheme clusters, which represent many visible character units.
-
Unicode Normalization and Text Equality
Normalization produces a prescribed form for canonically equivalent text so independently stored spellings can compare consistently.
-
Whitespace and Empty Fields in Text Records
Separators, padding, and meaningful space can coexist in one file. A parser needs an explicit rule for which to preserve.
-
Checking Prefixes and Suffixes of Text
A prefix must start at the first position, and a suffix must reach the last. A general substring can occur anywhere.
-
Choosing a List or Tuple for an Interface
Sequences can represent a variable list of items or a fixed arrangement of fields. Their mutability and positional meaning are separate design choices.
-
Rotating a Sequence by an Offset
A rotation changes the front of a sequence while preserving its cyclic order. Normalize the offset before splitting.
-
Longest Common Prefix of Strings
A longest common prefix stops where any supplied string ends or the next code points disagree.
-
Ordering a Sequence of Events
A sequence can store arrival order, time order, or another ordering rule. Choose consistently before computing neighbors or the latest event.