make_tree

pyinaturalist.models.make_tree(taxa, include_ranks, sort_key, root_id) Taxon

Organize a list of taxa into a taxonomic tree, defined by children and ancestors attributes. Expects exactly one root taxon.

Parameters:
  • taxa (Iterable[Taxon]) – Taxon objects to organize

  • sort_key (Callable[[Taxon], Any] | None) – Key function for sorting children; defaults to rank and name

  • include_ranks (list[str] | None) – If provided, only include taxa with these ranks; otherwise, include all ranks

  • root_id (int | None) – ID of the root taxon; if provided, only that taxon and its descendants will be included. Otherwise, the root taxon is determined automatically.

Return type:

Taxon

Returns:

Root taxon of the tree