Skip to content

Collections

Collections: the Python data structures you will use every day

Section titled “Collections: the Python data structures you will use every day”

Python ships four built-in collection types: list, tuple, dict, and set. As a TypeScript developer you already have mental models for all of them — the mapping is close, but each Python type has a distinct flavour.

PythonTypeScriptKey difference
listArrayMutable, ordered
tupleReadonly array / [T, U]Immutable, fixed-length
dictRecord<K,V> / object / MapMutable key-value
setSetUnique unordered values
TypeScript
// TypeScript Array
const nums: number[] = [1, 2, 3, 4, 5];
nums.push(6);
nums.splice(0, 1); // remove first element
console.log(nums[0]); // 2
console.log(nums.length); // 5
const doubled = nums.map((n) => n * 2);
const evens = nums.filter((n) => n % 2 === 0);
Python
# Python list
nums: list[int] = [1, 2, 3, 4, 5]
nums.append(6) # push
nums.pop(0) # remove first element
print(nums[0]) # 2
print(len(nums)) # 5
doubled = [n * 2 for n in nums] # list comprehension
evens = [n for n in nums if n % 2 == 0]

List comprehensions ([expr for x in iterable if condition]) are idiomatic Python — prefer them over map/filter for simple transformations.

TypeScript
// TypeScript object / Record
const user: Record<string, unknown> = {
name: "Alice",
age: 30,
};
user["role"] = "admin"; // add key
delete user["age"]; // remove key
console.log(user["name"]); // Alice
console.log(user["missing"] ?? "n/a"); // n/a (nullish coalescing)
for (const [k, v] of Object.entries(user)) {
console.log(k, v);
}
Python
# Python dict
user: dict[str, object] = {
"name": "Alice",
"age": 30,
}
user["role"] = "admin" # add key
del user["age"] # remove key
print(user["name"]) # Alice
print(user.get("missing", "n/a")) # n/a (safe lookup)
for key, value in user.items():
print(key, value)

Use .get(key, default) instead of dict[key] when the key might be absent — it returns the default instead of raising a KeyError.

TypeScript
// TypeScript Set
const tags = new Set<string>(["python", "web", "python"]);
tags.add("api");
console.log(tags.has("python")); // true
console.log(tags.size); // 3
tags.delete("web");
Python
# Python set — literal syntax with {}
tags: set[str] = {"python", "web", "python"} # duplicate removed
tags.add("api")
print("python" in tags) # True
print(len(tags)) # 3
tags.discard("web") # remove without error if absent

Slicing — Python’s killer feature for sequences

Section titled “Slicing — Python’s killer feature for sequences”

Python slicing lets you extract sub-sequences with [start:stop:step]. There is no direct TypeScript equivalent — .slice() exists on arrays but lacks the step parameter and negative indexing is not supported natively.

TypeScript
// TypeScript
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(nums.slice(2, 5)); // [2, 3, 4]
console.log(nums.slice(-3)); // [7, 8, 9]
// No step parameter in .slice()
Python
# Python slicing — works on list, tuple, and str
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums[2:5]) # [2, 3, 4] — indices 2,3,4
print(nums[-3:]) # [7, 8, 9] — last 3
print(nums[::-1]) # reversed — step of -1
print(nums[::2]) # [0,2,4,6,8] — every other
print(nums[1:8:2]) # [1,3,5,7] — start:stop:step
# Strings too!
s = "Hello, World!"
print(s[:5]) # Hello
print(s[-6:]) # World!
# list
nums = [1, 2, 3, 4, 5]
print(nums[1:4]) # [2, 3, 4]
print(nums[::-1]) # reversed
nums.append(6)
print(nums)
# list comprehension
doubled = [n * 2 for n in nums]
print(doubled)
# tuple (immutable)
point = (10, 20)
x, y = point
print(f"x={x}, y={y}")
# dict
user = {"name": "Alice", "age": 30}
user["role"] = "admin"
print(user.get("name"))
print(user.get("missing", "default"))
for k, v in user.items():
print(f" {k}: {v}")
# set
tags = {"python", "web", "python"}
tags.add("api")
print(tags)
print("python" in tags)
What Python collection type maps most closely to TypeScript's `Map<string, number>`?
What does `nums[::2]` return for `nums = [0,1,2,3,4,5,6]`?
Which method safely retrieves a value from a dict without raising KeyError?
You write `t = (1, 2, 3); t[0] = 99`. What happens?