How to Add a Type Hint for a List in Python
A type hint of plain list says a value is a list, but not what's in it. list[float] says both: a list whose items are floats. This guide adds one to a function that totals prices on Python 3.15.2, and shows what Python does and doesn't do with it.
Syntax
<name>: list[<item type>]
| Part | What it means |
|---|---|
<name> | A parameter or a variable, such as prices |
list | The built-in list type, written in lowercase |
[<item type>] | The type of every item, such as float, str or dict. One type only; for a mix, join types with | |
The typing documentation explains: "For most containers in Python, the typing system assumes that all elements in the container will be of the same type", with x: list[int] = [] as its example.
The Example
total() takes a list of prices and returns their sum. The script calls it with three prices and prints the hints Python stored on the function.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
def total(prices: list[float]) -> float:
return sum(prices)
print(total([2.5, 4.0, 1.25]))
print(total.__annotations__)
![The total.py script in a macOS-style code window: def total(prices: list[float]) -> float returns sum(prices); the script prints total of 2.5, 4.0 and 1.25, and total.annotations.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-list-type-hint-01-code.webp)
prices: list[float]says the argument is a list of floats.-> floatsays the function returns a float.total.__annotations__shows the hints as Python stored them.
Step 2: Run It
uv run python total.py
7.75
{'prices': list[float], 'return': <class 'float'>}
![The terminal after uv run python total.py: 7.75, then the annotations dictionary with prices as list[float] and return as class float.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-list-type-hint-02-output.webp)
The function returned 7.75, and Python kept the hint as list[float]. Python itself doesn't check the items: the hint is for readers, editors and type checkers. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing List[float] without importing it | The function runs, but reading its hints fails with NameError: name 'List' is not defined. Did you mean: 'list'? | Use the built-in list[float]. The docs call typing.List a "Deprecated alias to list" |
Listing two item types: list[float, str] | Python accepts it, but the typing docs mark it as a "Type checker error: list only accepts a single type argument" | For a mix, use list[float | str] |
| Expecting the hint to stop wrong items | total(["2.5", "4.0"]) still runs, and sum() fails with TypeError: unsupported operand type(s) for +: 'int' and 'str' | Convert values before the call, and let a type checker catch it earlier |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NameError: name 'List' is not defined when hints are read | List from typing wasn't imported | Switch to list[...] |
A TypeError from inside the function, despite the hint | The hint isn't enforced at run time, and the items are a different type | Check what the caller passes, such as text from a file instead of numbers |
FAQ
list[float] or typing.List[float]?
list[float]. The typing documentation says of typing.List: "Deprecated since version 3.9: builtins.list now supports subscripting ([])."
How do I hint a list that holds two kinds of items?
Join the types with |. def describe(values: list[float | str]) -> None: accepts [2.5, "n/a"], and its hints print as list[float | str].
Conclusion
prices: list[float] said total() takes a list of floats, and Python stored it as list[float] without checking the items. Use the lowercase built-in list, give it one item type, and use | when the items can be more than one type.
Additional Resources
- typing: Annotating tuples — how containers are hinted.
- Generic Alias Type — what
list[float]is at run time. - typing.List — the deprecated alias.
