Skip to content

Know your Python container types

Published on: December 24, 2023    Categories: Django, Python

This is the last of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction.

Python contains multitudes

There are a lot of container types available in the Python standard library, and it can be confusing sometimes to keep track of them all. So since it’s Christmas Eve and time to wrap any last-minute gifts, I’d like to wrap up this “Advent calendar” series with a guide to the most common types of data containers and what kinds of things you might want to wrap in them.

There are also other container types in the standard library — the collections module and the array module, for example, provide some specialized container types like Counter, which acts as a histogram structure, or array.array which works like a numeric-type array in C — but they’re more rarely used.

In general, my advice is: