A list is like a toy box, you can add or take out toys whenever you want. But an immutable list is like a sealed toy box, once it's closed, you can’t change what’s inside.
Like a Frozen Toy Box
Imagine your favorite toy box is full of blocks. If the box is sealed, and no one can open it, you can't add or remove any blocks. That's immutability, it's like a list that doesn’t let you change anything after it’s made.
Now imagine you have two sealed toy boxes: one has red blocks, and the other has blue blocks. If you want to make a new box with both colors, you can't just add to the old ones, you need to create a new box with all the blocks from both.
Why It Matters
This might seem silly, but in computer land, it helps everything run faster and stay clearer. Like when you're playing with blocks, knowing which boxes are sealed helps you know what's inside without opening them every time. A list is like a toy box, you can add or take out toys whenever you want. But an immutable list is like a sealed toy box, once it's closed, you can’t change what’s inside.
Like a Frozen Toy Box
Imagine your favorite toy box is full of blocks. If the box is sealed, and no one can open it, you can't add or remove any blocks. That's immutability, it's like a list that doesn’t let you change anything after it’s made.
Now imagine you have two sealed toy boxes: one has red blocks, and the other has blue blocks. If you want to make a new box with both colors, you can't just add to the old ones, you need to create a new box with all the blocks from both.
Examples
- A number that can't be changed, like a locked box.
- A variable in code that stays the same once set.
- Like a secret message that can’t be altered once written.
Ask a question
See also
- What are persistent data structures?
- What on Earth is Recursion? - Computerphile?
- What is immutability?
- What are continuations?
- How Does 2.4 Binary Shifts - Revise OCR GCSE Computer Science Work?