Starting with Python 3.9, you can use list[str] as a type annotation, which doesn't require importing anything, as documented in PEP 585.

Python 3.5 standardizes the way function annotations are used for type hinting, as documented in PEP 484. To annotate a list of strings, you use List[str], where List is imported from the typing module. You can also use Sequence[str] if your function accepts any list-like sequence, or Iterable[str] for any iterable.

Python 3.4 and earlier doesn't specify a format for its function annotations, it merely provides a mechanism that allows you to use any expression as the annotation. How the annotations are interpreted is up to you and the libraries you use.

Answer from interjay on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 week ago - For this reason, tuples are special-cased in Python’s typing system. tuple accepts any number of type arguments: # OK: ``x`` is assigned to a tuple of length 1 where the sole element is an int x: tuple[int] = (5,) # OK: ``y`` is assigned to a tuple of length 2; # element 1 is an int, element 2 is a str y: tuple[int, str] = (5, "foo") # Error: the type annotation indicates a tuple of length 1, # but ``z`` has been assigned to a tuple of length 3 z: tuple[int] = (1, 2, 3)
Discussions

Type Hints, can you indicate various types inside a list?
You could use List[Union[str, int]] for a list that contains strings and ints, but that is not what you have here. This is actually a list of tuples, each of which has a str and an int. For that you can use List[Tuple[str, int]] which specifically says that each tuple contains two items, the first of which is a string and the second an int. Also note that on Python 3.9+ you should use the built-in types rather than the ones defined in typing, so list[tuple[str, int]]. And note again that in your example list_of_grades[0] isn't a string, it's a tuple of (string, int). More on reddit.com
🌐 r/learnpython
7
2
November 15, 2021
Recursive type annotation for a nested list of lists of lists of - Typing - Discussions on Python.org
I’m trying to figure out the way to write type annotations to represent a type nested[T,N] such that: nested[T,0] = list[T] nested[T,1] = list[list[T]] nested[T,2] = list[list[list[T]]] etc Is there a good way of doing this? I have hundreds of functions with signatures mostly being like: ... More on discuss.python.org
🌐 discuss.python.org
0
December 11, 2024
python - Type hinting a collection of a specified type - Stack Overflow
Using Python 3's function annotations, it is possible to specify the type of items contained within a homogeneous list (or other collection) for the purpose of type hinting in PyCharm and other IDE... More on stackoverflow.com
🌐 stackoverflow.com
Do you use type annotations (Python 3.6+)?
Depends on context. If I'm writing something really short/throwaway, then probably not. For larger systems and "enterprise" programming, I advocate using them. I use PyCharm, and one of the nice advantages of using type annotations is that it allows PyCharm to highlight type mismatches, helping to avoid certain classes of problems. It also allows for better tab completion of complex types. More on reddit.com
🌐 r/Python
31
25
April 29, 2019
🌐
Mypy
mypy.readthedocs.io › en › stable › cheat_sheet_py3.html
Type hints cheat sheet - mypy 1.19.1 documentation
# For most types, just use the name of the type in the annotation # Note that mypy can usually infer the type of a variable from its value, # so technically these annotations are redundant x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # For collections on Python 3.9+, the type of the collection item is in brackets x: list[int] = [1] x: set[int] = {6, 7} # For mappings, we need the types of both keys and values x: dict[str, float] = {"field": 2.0} # Python 3.9+ # For tuples of fixed size, we specify the types of all the elements x: tuple[int, str, float] = (3, "yes
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
These "type hints" or annotations are a special syntax that allow declaring the type of a variable. By declaring types for your variables, editors and tools can give you better support. This is just a quick tutorial / refresher about Python type hints. It covers only the minimum necessary to use them with FastAPI...
🌐
Reddit
reddit.com › r/learnpython › type hints, can you indicate various types inside a list?
r/learnpython on Reddit: Type Hints, can you indicate various types inside a list?
November 15, 2021 -

If I have a list that I know will have a certain type of object in the first element and a different type in the second. For instance, if I have a function that combines two lists and returns a lists of lists like so:

names = ["Brad Pitt", "Leonardo DiCaprio", "Henry Cavill", "Hugo Weaving"]
grades = [85,32,95,90]

def combine_lists():
    names_grades = []
    for i in range(0,4):
        names_grades.append(names[i],grades[i])
    return names_grades

Is it possible to type hint the return type so that it reflects the types in the list, something like this:

def combine_lists() ->List(str,int):

The point of this is that later I would be aware of the type I'm accessing when I try to read a specific element of the list for example:

list_of_grades = combine_lists()x = list_of_grades[0] #I want to know this is a string

🌐
LogRocket
blog.logrocket.com › home › understanding type annotation in python
Understanding type annotation in Python - LogRocket Blog
June 4, 2024 - Starting with Python ≥3.9, to annotate a list, you use the list type, followed by []. [] contains the element’s type data type.
🌐
Python
peps.python.org › pep-0484
PEP 484 – Type Hints | peps.python.org
In order for maximal compatibility with offline type checking it may eventually be a good idea to change interfaces that rely on annotations to switch to a different mechanism, for example a decorator. In Python 3.5 there is no pressure to do this, however. See also the longer discussion under Rejected alternatives below. No first-class syntax support for explicitly marking variables as being of a specific type is added by this PEP. To help with type inference in complex cases, a comment of the following format may be used: x = [] # type: List[Employee] x, y, z = [], [], [] # type: List[int], List[int], List[str] x, y, z = [], [], [] # type: (List[int], List[int], List[str]) a, b, *c = range(5) # type: float, float, List[float] x = [1, 2] # type: List[int]
Find elsewhere
🌐
Medium
medium.com › @ramanbazhanau › advanced-type-annotations-in-python-part-1-3c9a592e394
Advanced Type Annotations in Python | Medium | Medium
September 20, 2023 - In this article, we'll dive deep into some of the advanced type annotations available in Python. TypeVar is used to define a type variable. This is useful when you want to enforce the same type across multiple parts of a function or class. from typing import List, TypeVar T = TypeVar('T') def first_and_last(items: List[T]) -> T: return items[0] result = first_and_last([1, 2, 3, 4]) # result: int
🌐
Dagster
dagster.io › blog › python-type-hinting
Using Type Hinting in Python Projects
You will also find a list of Python's main types in the appendix. In Python, there is a distinction between atomic and composite types when it comes to type hinting. Atomic types, such as int, float, and str, are simple and indivisible, and their type annotations can be provided directly using ...
🌐
Python.org
discuss.python.org › typing
Recursive type annotation for a nested list of lists of lists of - Typing - Discussions on Python.org
December 11, 2024 - I’m trying to figure out the way to write type annotations to represent a type nested[T,N] such that: nested[T,0] = list[T] nested[T,1] = list[list[T]] nested[T,2] = list[list[list[T]]] etc Is there a good way of doing this? I have hundreds of functions with signatures mostly being like: def func1(p: nested[T,0]) -> T: ... def func2(p: nested[T,0], q: nested[T,0]) -> nested[T,0]: ... def func3(p: nested[T,N], q: nested[T,N], N: int) -> nested[T,N]: ... def func4(p: nested[T,N], ...
Top answer
1 of 5
346

As of May 2015, PEP 484 (Type Hints) has been formally accepted. The draft implementation is also available at github under ambv/typehinting.

In September 2015, Python 3.5 was released with support for Type Hints and includes a new typing module. This allows for the specification of types contained within collections. As of November 2015, JetBrains PyCharm 5.0 fully supports Python 3.5 to include Type Hints as illustrated below.

from typing import List

def do_something(l: List[str]):
    for s in l:
        s  # str

Original Answer

As of Aug 2014, I have confirmed that it is not possible to use Python 3 type annotations to specify types within collections (ex: a list of strings).

The use of formatted docstrings such as reStructuredText or Sphinx are viable alternatives and supported by various IDEs.

It also appears that Guido is mulling over the idea of extending type annotations in the spirit of mypy: http://mail.python.org/pipermail/python-ideas/2014-August/028618.html

2 of 5
228

As of Python 3.9, builtin types are generic with respect to type annotations (see PEP 585). This allows to directly specify the type of elements:

def my_func(l: list[int]):
    pass

This also extends to most other container types of the standard library, for example collections.deque or collections.abc.Mapping.


Various tools may support this syntax earlier than Python 3.9. When annotations are not inspected at runtime, the syntax is valid using quoting or __future__.annotations.

# quoted
def my_func(l: 'list[int]'):
    pass
# postponed evaluation of annotation
from __future__ import annotations

def my_func(l: list[int]):
    pass

As a consequence of PEP 585, most helpers in typing corresponding to standard library types are deprecated, such as typing.List, typing.Deque or typing.Mapping. They should only be used if compatibility with Python versions prior to 3.9 is required.

🌐
University of Toronto
teach.cs.toronto.edu › ~csc148h › winter › notes › python-recap › type_annotations.html
1.4 Python Type Annotations
Sometimes we want to be flexible and say that a value must be a list, but we don’t care what’s in the list (e.g. it could be a list of strings, a list of integers, a list of strings mixed with integers, etc.). In such cases, we can simply use the built-in types list, dict, and tuple for these types. Now that we know how to name the various types, let’s see how we can use this to annotate the type of a function.
🌐
Pybites
pybit.es › articles › code-better-with-type-hints-part-2
Code Better With Type Hints – Part 2 - Pybites
August 30, 2021 - If you want to type annotate a dictionary, you have to provide type hints for both the key and the values, e.g. dict[str, list] for a dictionary that uses strings as its keys and a list as its values.
🌐
FastAPI Tutorial
fastapitutorial.com › blog › advanced-type-hints
2. Advanced Type Hints
In the newer versions of Python, we can directly use the list, tuple, and dict keywords. new_price: list[int] = [14,902,898] new_immutable_price: tuple[int,int,int] = (388,392,299) new_price_dict: dict[str,int] = { "item_1":240, "item_2":490, } 2. Annotating with complex data structures ⚠️ For now, I am proceeding with the older and established syntax for type annotations.
🌐
University of Toronto
teach.cs.toronto.edu › ~csc148h › notes › python-recap › type_annotations.html
1.5 Python Type Annotations — CSC148 Course Notes
In previous versions of Python (and offerings of CSC148), we had to use type annotations List, Dict, and Tuple imported from the typing module.
🌐
GeeksforGeeks
geeksforgeeks.org › python › specify-argument-type-for-list-of-dictionary-for-a-python
Specify Argument Type For List of Dictionary For a Python - GeeksforGeeks
July 23, 2025 - So, In general, we can write a function in Python as: ... But here, we don't know if the function 'add' accepts int or float values. PEP 3107 Function Annotations now enable us to write our code as: ... Now we can easily tell by reading the code what is the expected data type of the parameters and output. So, now for Specifying Argument Type for List ...
🌐
Python
typing.python.org › en › latest › spec › annotations.html
Type annotations — typing documentation
| <Final> ('[' annotation_expr... maybe_unpacked)* ']' string_annotation ::= string (must be a string literal that is parsable as Python code; see "String annotations") type_expression_list ::= '[' type_expression (',' type_expression)* ']' | '[' ']'...
🌐
Python
docs.python.org › 3.10 › library › typing.html
typing — Support for type hints — Python 3.10.19 documentation
A generic version of dict. Useful for annotating return types. To annotate arguments it is preferred to use an abstract collection type such as Mapping. ... Deprecated since version 3.9: builtins.dict now supports subscripting ([]). See PEP 585 and Generic Alias Type. ... Generic version of list.
🌐
flycoolman
flycoolman.com › coding › python › list-tuple-dict-vs-list-tuple-dict
list, tuple, dict vs List, Tuple, Dict | flycoolman
October 4, 2020 - Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. Here’s an example of adding type information to a function. You annotate the arguments and the return value: