Data structures to represent data in Python

August 16, 2022

Comments from Nic:

I see it as a hierarchy of needs where I choose the "simplest" for the use case required:

  • tuple - frozen list
  • collections.namedtuple - tuple with labels for positions (I use this for when I want to return multiple objects that don't really go together normally, eg pytest example test case parameters)
  • typing.NamedTuple - namedtuple with type hints
  • dataclasses - for cases where the object can be thought of as a genuine data model
  • pydantic - for when you have a data model and want validation, conversion and serialisation included

Given that we use pydantic extensively already and I like validation the gap in use cases between typing.NamedTuple and pydantic is quite small in my mind which is why I don't default to using them much.