PYTHON DATA STRUCTURES · 11 MIN
Lists, Tuples, Sets & Dictionaries
Python's four built-in collections each fit a different job.
PYRA explains
- list: ordered and changeable — append(), pop(), sort().
- tuple: ordered but fixed — good for pairs like (x, y).
- set: unique items with fast membership checks. dict: key → value lookups, e.g. scores["asha"].
Real world: A shopping list (list), GPS coordinates (tuple), unique visitors (set), a phone book (dict).
Code
nums = [3, 1, 3, 2]
nums.append(5)
print(sorted(set(nums)))
ages = {"asha": 21, "ravi": 19}
ages["zoya"] = 22
print(len(ages), ages.get("ravi"))Output ›
[1, 2, 3, 5]
3 19Try it in the online compiler →Checkpoint quiz
Best structure to check quickly whether a user ID was already seen?
What does {"a": 1}.get("b") return?