Lists are everywhere in Python. But are they always the best tool for the job?
When writing Python, we often to need to work with a collection of objects. And so often we reach for the humble list. But lists, while they can get the job done, are often not the best choice.
Do you really need that collection to change? If you don’t, picking a tuple instead will bring unexpected benefits. Or is the order of the elements important? If not, then using a set will open up a world of new possibilities. And if your set doesn’t need to change, then consider a frozenset - Python’s most underrated built-in data structure.
Equipped with these data structures, we’ll close by looking at the subtleties of how to type-annotate functions, and why on earth it is better to use Set
(with a capital ‘S’) for an argument type, but set
for the return type.
It’s time to go beyond lists!
Comments