Quickie Dev #2: Lists, sets, dicts, tuples… in Python

Because choosing the right data structure can save your program!

Mina Pêcheux
Nerd For Tech
Published in
9 min readMay 3, 2021

--

When you want to store a pack of data in Python, you can pick one of several built-in iterable types (in the docs, they’re classified in “sequence”, “set” and “mapping” types). Most of us know of the lists and the dicts, but there are some others that can prove very efficient in specific situations.

It’s important to know the strengths and weaknesses of those common data types because choosing the proper data structure at the right time can drastically alter (in good or in bad) the performance of your application!

Lists, sets, tuples, dicts: what are they?

I’ll dive more into each of these data types and their properties in the following section, but as a quick intro:

  • lists are mutable sequence types denoted by square brackets [] and they store contiguous elements (potentially of different types) ordered by integer indices
  • sets are denoted by brackets {} and they're an unordered collection of unique hashable elements - you may add or remove elements from a set
  • a frozenset is almost identical, but once you’ve created it you can’t add or remove elements from it anymore…

--

--

Mina Pêcheux
Nerd For Tech

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)