3.9. Sequence Type Annotation

3.9.1. Tuple

Listing 3.42. Generic type annotation
data: tuple = ()
data: tuple = tuple()

data: tuple = 'a', 2, 3.3
data: tuple = ('a', 2, 3.3)
Listing 3.43. Explicit type annotation
from typing import Tuple

data: Tuple[int, int, int] = (1, 2, 3)
data: Tuple[str, str, str] = ('setosa', 'virginica', 'versicolor')
data: Tuple[str, int, float] = ('a', 2, 3.3)

3.9.2. List

Listing 3.44. Generic type annotation
data: list = list()
data: list = []

data: list = ['a', 1, 2.2]
Listing 3.45. Explicit type annotation
from typing import List

data: List[int] = [1, 2, 3, 4]
data: List[float] = [5.8, 2.7, 5.1, 1.9]
data: List[str] = ['a', 'b', 'c', 'd']

3.9.3. Set

Listing 3.46. Generic type annotation
data: set = set()

data: set = {'a', 1, 2.2}
Listing 3.47. Explicit type annotation
from typing import Set

data: Set[int] = {1, 2, 3}
data: Set[float] = {0.0, 1.1, 2.2}
data: Set[str] = {'a', 'b', 'c'}

3.9.4. Frozenset

Listing 3.48. Generic type annotation
data: frozenset = set()

data: frozenset = {'a', 1, 2.2}
Listing 3.49. Explicit type annotation
from typing import FrozenSet

data: FrozenSet[int] = {1, 2, 3}
data: FrozenSet[float] = {0.0, 1.1, 2.2}
data: FrozenSet[str] = {'a', 'b', 'c'}

3.9.5. List of Tuples

Listing 3.50. Generic type annotation
from typing import List


data: List[tuple] = [
    (4.7, 3.2, 1.3, 0.2, 'setosa'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
]
Listing 3.51. Explicit type annotation
from typing import List, Tuple


data: List[Tuple[float, float, float, float, str]] = [
    (4.7, 3.2, 1.3, 0.2, 'setosa'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
]

3.9.6. List of Lists

Listing 3.52. Generic type annotation
from typing import List

data: List[list] = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]
Listing 3.53. Explicit type annotation
from typing import List

data: List[List[int]] = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

3.9.7. Aliases

from typing import List, Tuple


GeographicCoordinate = Tuple[float, float]

locations: List[GeographicCoordinate] = [
    (25.91375, -60.15503),
    (-11.01983, -166.48477),
    (-11.01983, -166.48477)
]
from typing import List, Tuple


Iris = Tuple[float, float, float, float, str]

data: List[Iris] = [
    (4.7, 3.2, 1.3, 0.2, 'setosa'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
]

3.9.8. Unions

Listing 3.54. Generic type annotation
from typing import Union


data: List[Union[list, tuple, set]] = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
]
Listing 3.55. Explicit type annotation
from typing import Union, List, Tuple, Set


data: List[Union[List[int], Tuple[int, int, int], Set[int]]] = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
]
Listing 3.56. Explicit type annotation
from typing import Union, List, Tuple, Set


Row = Union[List[int], Tuple[int, int, int], Set[int]]

data: List[Row] = [
    [1, 2, 3],
    (4, 5, 6),
    {7, 8, 9},
]

3.9.9. Future

New in version Python: 3.9 PEP 585 Will be possible to use list[int], set[int] etc without importing from typing

data: list[int] = [1, 2, 3]
data: set[int] = {1, 2, 3}

3.9.10. More Information

Note

More information in Type Annotations and Type Checking