How to choose a Data Structure in Python

Python has a number of built-in Data Structures to work with, each one with its own features.

Here are the reasons to choose a particular Data Structure over the other:

  • Lists: if you need your items to be ordered, and you need the flexibility of a list to add, remove, and update items. You can learn about lists on this article Python List: a quick reference.
  • Tuples: if you need your items to be ordered, but the items won’t change, so you can have a more performant access to them. You can learn about tuples on this article Python Tuple: a quick reference.
  • Sets: when you don’t care about the order of your items and you want to make sure you don’t have duplicates. You can learn about sets on this article Python Set: a quick reference.
  • FrozenSets: no order in the items, and no duplicates, but the items won’t change, so you have more performant access to them. I talk briefly about Frozen Sets on this article Python Data Types
  • Dictionaries: prior to Python 3.6 order doesn’t matter, but from Python 3.6 forward dict keeps insertion order. You need to associate key-value pairs, and be able to lookup for data based on a custom key instead of indexes. You can learn about dictionaries on this article Python Dictionary: a quick reference

Watch this Content