Python for JS Devs · Fundamentals

Collections: list, dict, tuple, set

Your array and object have four Python cousins. Knowing which to reach for is half of writing idiomatic Python.

Lesson 03Time ~13 min Win Use all four fluently in the REPLPrereq Lesson 02

JS gives you Array, Object, Map, Set. Python gives you list, dict, tuple, set. The mapping is close but not 1:1 — and the mismatches are where the value is.

PythonClosest JSLiteralMutable?
listArray[1, 2, 3]yes
dictMap / Object{"a": 1}yes
tuplefrozen Array(1, 2)no
setSet{1, 2, 3}yes

list — the workhorse array

JavaScript
xs = [1, 2, 3];
xs.push(4);
xs.length;
xs.includes(2);
xs.slice(1, 3);
Python
xs = [1, 2, 3]
xs.append(4)     # not push
len(xs)          # len() function, not .length
2 in xs          # `in`, not .includes
xs[1:3]         # slicing syntax

Slicing is a Python superpower: xs[start:stop:step], and negatives count from the end. xs[-1] is the last item; xs[::-1] reverses.

dict — not the same as a JS object

A JS object doubles as "struct" and "map". A Python dict is purely a map — and you access it with brackets, not dots.

JavaScript
u = { name: "kd", age: 30 };
u.name;
u.email ?? "none";
"name" in u;
Object.keys(u);
Python
u = {"name": "kd", "age": 30}
u["name"]              # KeyError if missing!
u.get("email", "none")  # safe default (?? equivalent)
"name" in u             # checks keys
u.keys(), u.values(), u.items()
The trap u["email"] on a missing key raises KeyError — it does not return undefined. Use u.get("email") (returns None) or u.get("email", default) when the key might be absent.

tuple — the one with no JS equivalent

An immutable, fixed-shape sequence. You'll meet it everywhere: returning multiple values, dict keys, fixed records. Unpacking is the killer feature (like array destructuring, but pervasive).

point = (10, 20)
x, y = point            # unpacking, like [x, y] = arr

def min_max(xs):
    return min(xs), max(xs)   # returns a tuple

low, high = min_max([3, 1, 9])   # unpack the result

set — dedup & membership

seen = {1, 2, 2, 3}    # → {1, 2, 3}
3 in seen                # O(1) membership
a | b                    # union     a & b → intersection
set([1,1,2])            # dedup a list
Gotcha {} is an empty dict, not a set. The empty set is set().

🎯 Your win

In the REPL: make a list of user dicts users = [{"name":"a"}, {"name":"b"}], then (1) safely read users[0].get("email", "n/a"), (2) unpack first, second = users, (3) build a set of names and confirm "a" in names. If all four collection types feel usable, you're set.

Check yourself

Q1. Accessing a missing dict key with d["x"] does what?
Bracket access on a missing key raises KeyError. Use d.get("x") for a safe None.
Q2. Which literal creates an empty set?
{} is an empty dict. The empty set must be written set().
Q3. What does xs[-1] return?
Negative indices count from the end; xs[-1] is the last item.
Primary source Python docs — Data Structures. The canonical tour of list/dict/tuple/set methods.
💬 Ask your teacher: when should you pick a tuple over a list? Great question to bring.