DEV Community

Fidel Okumu
Fidel Okumu

Posted on

PYTHON Data Structures lists, Tuples, Dictionaries and Sets

INTRODUCTION
After learning storing single values in variables, We needed a way to hold collection of data i.e multiple ride counts
multiple cities etc.
Python offers four main data structures for this that is:

LISTS
A list is a built-in data type used to store multiple items in a single variable.
A list can be changed and allows addition of more items
It can also contain items of different data types
Lists also preserve and allows duplicate values



![ ](https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/njfzoohpy0tokgot2si1.png

TUPLES
A tuple is similar to a list but once created, it cannot be changed.

Trying to modify a tuple raises an error by design.
Tuples are mainly used when values should stay fixed throughout a program example: routes fixed origin and destinations.

DICTIONARIES
A dictionary stores data in key-value.
This allows lookups by name instead of position
example;

Dictionaries are written in curly brackets {}
Each has a corresponding value
The keys should be unique
Dictionaries can be changed

SETS
A set stores only unique values any duplicates are automatically removed
They are written using curly brackets {}
You can add or remove items in a set
They are useful when keeping unique items

What I Learned
The most useful shift in understanding was realizing that choosing a data structure is really a design decision, not just a syntax choice. Picking a list when a dictionary is more appropriate (or vice versa) doesn't just look sloppy β€” it makes later code longer and harder to reason about. Matching the structure to what the data actually needs to do made every function I wrote afterward noticeably simpler.

Top comments (0)