Search code examples
pythonmypyhashable

Immutable hashable list with correct typings


I need to use sequence of items as dict key and to feat List[...] type. If I use tuple, than it does not feat to a List[...] type and I cannot use Tuple[...] type, because tuple length is not known.

Is there any class (maybe from a 3rd party package) to be like hashable frozen list?


Solution

  • The immutable equivalent to List[T] is Tuple[T, ...].

    From the Python documentation:

    To specify a variable-length tuple of homogeneous type, use literal ellipsis, e.g. Tuple[int, ...]. A plain Tuple is equivalent to Tuple[Any, ...], and in turn to tuple.