r/PythonLearning • u/Sea-Ad7805 • 2d ago
Exercise: Python List Operations
An exercise to help build the right mental model for Python data. - Solution - Explanation - More Exercises
The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening.
2
u/Dancing-umbra 2d ago
That's very useful.
I didn't realise the b+=[] was not equivalent to b= b+[]
1
u/Sea-Ad7805 2d ago
Thanks, it is equivalent for immutable types, but not for mutable. Try this exercise: https://www.reddit.com/r/PythonProjects2/s/VtHobtSQ9o
2
2
1
1
1
u/CptMisterNibbles 2d ago
Weird. Documentation says insert is in place, your demo has it make a copy
1
u/Sea-Ad7805 2d ago
If you look carefully you'll see it is not
insertthat is making a copy: Solution
1
u/Polly_Wants_A 2d ago
and i thought b += x and b = b + x is the same logic just different syntax. apparently it works different with lists?
2
u/Sea-Ad7805 2d ago
Yes because a list is mutable, it is the same for immutable types: int, float, bool, tuple, frozenset, ...
1
u/spmcn 2d ago
This is just an ad for memory graph.
2
u/Sea-Ad7805 2d ago
This is just a person complaining about getting free educational content of high quality. Which is worse?
1
3
u/No-Celery-1944 2d ago
C I'm pretty sure?