Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner18 min

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.

Mutable hub — list set dict bytearray around Same ID center

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

List same id=5001 — lst[0]=99 and append(40) becomes [99,20,30,40]
python
lst = [10, 20, 30]

old_id = id(lst)

lst[0] = 99
lst.append(40)

print(lst)
print(id(lst) == old_id)

Output:

text
[99, 20, 30, 40]
True

Common 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

Set same id() — add(4) remove(1) changes membership in place
python
s = {1, 2, 3}

old_id = id(s)

s.add(4)
s.remove(1)

print(s)
print(id(s) == old_id)

Output:

text
{2, 3, 4}
True

Useful 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

Dict same id() — update year and add creator key in place
python
d = {"name": "Python", "year": 1991}

old_id = id(d)

d["year"] = 2024
d["creator"] = "Guido"

print(d)
print(id(d) == old_id)

Output:

text
{'name': 'Python', 'year': 2024, 'creator': 'Guido'}
True

Common 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.

bytearray same id() — ba[2]=90 changes C to Z in place
python
ba = bytearray(b"ABC")

ba[2] = 90

print(ba)

Output:

text
bytearray(b'ABZ')

Useful when working with binary files and network packets.

See: bytearray · bytes

Reference sharing (important concept)

This is where mutable objects become interesting. Two variables can point to the same object.

a and b both point to id=8001 list — a.append(4) changes both views
python
a = [1, 2, 3]

b = a

a.append(4)

print(a)
print(b)

Output:

text
[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 —
Mutable list set dict bytearray vs Immutable tuple bytes frozenset str

Playground

Run this complete example.

python
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

  1. Which types are mutable?
    Answer: list, set, dict, and bytearray.

  2. Does lst.append(4) usually change id(lst)?
    Answer: No — the existing list is modified in place.

  3. Can you assign d["key"] = value?
    Answer: Yes — dictionaries are mutable mappings.

30-second interview revision

Mutable hub — list set dict bytearray with Same object Same id()
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…