We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Nested Types

We've looked at relatively simple container types like list[str], but they can get more complex when one container holds another container. That is, it's possible to have nested container types.

A dictionary, for example, could map each character's name to their list of spells:

character_spells: dict[str, list[str]] = {
    "Gandalf": ["Fireball", "Light"],
    "Frodo": ["Hide"],
}

We read dict[str, list[str]] from the outside in:

  • It's a dictionary (dict)
  • Each key is a string (str)
  • Each value is a list of strings (list[str])

In extreme cases, nested types can get super confusing, but honestly it's less confusing than it would be without the typing. For the time being, just know that type hints can describe containers within containers.