Meaning
Represents a list where each element is a tuple containing a string and an integer. Used when you need a collection of key‑value‑like pairs where the key is a string and the value is an integer, e.g., labeling items with counts.
Primary Function
Type annotation
Communicative Purpose
Specifies the expected structure of a collection of string‑int pairs for static type checking.
Pattern
List[Tuple[str, int]]
Core Structure
List[Tuple[str, int]]
Função primária
Type annotation
Propósito comunicativo
Specifies the expected structure of a collection of string‑int pairs for static type checking.
Situações de gatilho
Defining function parameters or return types that hold labeled counts; storing results like word frequencies; processing configuration entries.
Contextos
Python codebases using the typing module, data‑processing scripts, APIs returning aggregated statistics.
Padrão
List[Tuple[str, int]]
Estrutura central
List[Tuple[str, int]]
Slots de substituição
first_type: type (e.g., str), second_type: type (e.g., int)
Colocados típicos
- typing.List
- typing.Tuple
- function signatures
- return annotations
Substituições comuns
- List[Tuple[int
- str]] (swapped)
- List[Dict[str
- int]]
- List[Tuple[str
- float]]
Erros comuns
Forgetting to import Tuple and List from typing (Python <3.9); using list literal syntax incorrectly; confusing with List[List[int]].
Similar / contraste
List[Tuple[str, str]] (string‑string pairs), Dict[str, int] (mapping), List[int] (simple list).
Interferências
Coming from languages with built‑in associative arrays (e.g., JavaScript objects): may expect a dict instead of a list of tuples.
Família do chunk
- List[Tuple[str
- str]]
- List[Tuple[int
- int]]
- List[Tuple[str
- float]]
Nuance
In Python 3.9+ you can use builtin list and tuple: list[tuple[str, int]]; the pattern is equivalent but requires from __future__ import annotations for older versions.
Efeito pragmático
Makes intent explicit, enables static type checkers to catch mismatched types.
Dica de memória
Think of a grocery list: each item is a name (string) and its quantity (int).
Nota
Can be used with sorting functions to sort by count then label, e.g., sorted(pairs, key=lambda x: (-x[1], x[0]))
Upgrade path
Use list[tuple[str, int]] (Python 3.9+) or consider TypedDict/NamedTuple for more structured data.
Log in to save chunks.