Python for JS Devs · Fundamentals
Your array and object have four Python cousins. Knowing which to reach for is half of writing idiomatic Python.
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.
| Python | Closest JS | Literal | Mutable? |
|---|---|---|---|
list | Array | [1, 2, 3] | yes |
dict | Map / Object | {"a": 1} | yes |
tuple | frozen Array | (1, 2) | no |
set | Set | {1, 2, 3} | yes |
xs = [1, 2, 3];
xs.push(4);
xs.length;
xs.includes(2);
xs.slice(1, 3);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 syntaxSlicing is a Python superpower: xs[start:stop:step], and negatives count from the end. xs[-1] is the last item; xs[::-1] reverses.
A JS object doubles as "struct" and "map". A Python dict is purely a map — and you access it with brackets, not dots.
u = { name: "kd", age: 30 };
u.name;
u.email ?? "none";
"name" in u;
Object.keys(u);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()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.
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
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
{} is an empty dict, not a set. The empty set is set().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.
d["x"] does what?KeyError. Use d.get("x") for a safe None.{} is an empty dict. The empty set must be written set().xs[-1] return?xs[-1] is the last item.