Lesson 03
Mutable Types in Python
Learn how list, set, dict, and bytearray modify data in place, keep the same id(), and affect every variable pointing to the same object.
What you'll learn
| Type | In-place operations |
|---|---|
list |
append(), index assignment |
set |
add(), remove() |
dict |
Key assignment and update |
bytearray |
Mutable bytes |
After Immutable Types, you know which types cannot change. This lesson covers the four built-in types that can.
Visual hub of mutable types
All these types allow editing without creating a new object.
Definition: A mutable object's content changes at the same memory address.
1. list — editable sequence
Lists are the most common mutable type.
Visual flow
lst = [10, 20, 30]
old_id = id(lst)
lst[0] = 99
lst.append(40)
print(lst)
print(id(lst) == old_id)Output:
[99, 20, 30, 40]
TrueCommon mutation methods
| Method | Purpose |
|---|---|
append() |
Add one item |
extend() |
Add many items |
insert() |
Insert at index |
pop() |
Remove by index |
remove() |
Remove by value |
clear() |
Remove everything |
See: list
2. set — editable unique collection
Sets store unique values and can grow or shrink.
Visual flow
s = {1, 2, 3}
old_id = id(s)
s.add(4)
s.remove(1)
print(s)
print(id(s) == old_id)Output:
{2, 3, 4}
TrueUseful methods
| Method | Action |
|---|---|
add() |
Insert value |
remove() |
Delete value |
discard() |
Safe remove (no error if missing) |
clear() |
Empty set |
See: set
3. dict — editable key-value mapping
Dictionaries let you modify values using keys.
Visual flow
d = {"name": "Python", "year": 1991}
old_id = id(d)
d["year"] = 2024
d["creator"] = "Guido"
print(d)
print(id(d) == old_id)Output:
{'name': 'Python', 'year': 2024, 'creator': 'Guido'}
TrueCommon methods
| Method | Purpose |
|---|---|
update() |
Merge dictionaries |
pop() |
Remove key |
keys() |
All keys |
values() |
All values |
items() |
Key-value pairs |
See: dict
4. bytearray — mutable bytes
bytes is immutable. bytearray is its editable version.
ba = bytearray(b"ABC")
ba[2] = 90
print(ba)Output:
bytearray(b'ABZ')Useful when working with binary files and network packets.
Reference sharing (important concept)
This is where mutable objects become interesting. Two variables can point to the same object.
a = [1, 2, 3]
b = a
a.append(4)
print(a)
print(b)Output:
[1, 2, 3, 4]
[1, 2, 3, 4]Because a and b refer to the same list, changing one changes the other.
This concept leads into Aliasing and Copying — copy vs shallow copy vs deep copy.
Mutable vs immutable pairs
| Mutable | Immutable partner |
|---|---|
list |
tuple |
set |
frozenset |
bytearray |
bytes |
dict |
— |
Playground
Run this complete example.
lst = [10, 20, 30]
print("id:", id(lst))
lst.append(40)
print("after append:", lst, "id:", id(lst))
d = {"a": 1}
d["b"] = 2
print(d)Observe:
append()updates the same list- Dictionary gains a new key
id()remains unchanged
Use the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
-
Which types are mutable?
Answer:list,set,dict, andbytearray. -
Does
lst.append(4)usually changeid(lst)?
Answer: No — the existing list is modified in place. -
Can you assign
d["key"] = value?
Answer: Yes — dictionaries are mutable mappings.
30-second interview revision
| Type | Key mutation |
|---|---|
list |
append(), pop() |
set |
add(), remove() |
dict |
d[key] = value |
bytearray |
Index assignment |
Golden rule: Mutable objects are edited, not replaced. Multiple variables pointing to the same mutable object will all observe the same changes.
Next: Item Assignment and id().
Loading playground…