Skip to main content

How to Add a Type Hint for a List in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<name>: list[<item type>]
PartWhat it means
<name>A parameter or a variable, such as prices
listThe 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​

total.py
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.

  • prices: list[float] says the argument is a list of floats.
  • -> float says the function returns a float.
  • total.__annotations__ shows the hints as Python stored them.

Step 2: Run It​

Terminal
uv run python total.py
Output
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.

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​

MistakeWhat happensDo this instead
Writing List[float] without importing itThe 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 itemstotal(["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​

SymptomLikely causeFix
NameError: name 'List' is not defined when hints are readList from typing wasn't importedSwitch to list[...]
A TypeError from inside the function, despite the hintThe hint isn't enforced at run time, and the items are a different typeCheck 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​