Python — Lists VS Dicts VS Sets VS Tuples
2 min readSep 5, 2024
Today we’re going back to basics.
I recently went back to school, and have to take a compulsory Python programming module. Which has been reinforcing my absolute basics once again (it’s been 7 years)
And one topic that has been confusing my classmates who are new to programming is: when to use lists VS dicts VS sets VS tuples
Lists
Ordered, Mutable Sequence
mylist = ['apple', 'orange', 'pear']
When to use lists:
- when we want to keep track of the ORDER of elements.
- when we need to sometimes add/delete stuff to/from our list
Dicts
Collection of key value pairs
mydict = {"apple":4, "orange":5}
When to use dicts:
- when we don’t care about ORDER of elements
- when we want to access a value using a key time-efficiently
Tuples
Lists but immutable. Ordered, Immutable Sequence
And we use () instead of []
Immutable means we cannot change it after we create it
mytuple = ("apple", "orange", "pear")