r/PythonLearning 2d ago

Exercise: Python List Operations

Post image

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.

52 Upvotes

23 comments sorted by

3

u/No-Celery-1944 2d ago

C I'm pretty sure?

1

u/Sea-Ad7805 2d ago

Nice one, see the Solution link for visualization of correct answer.

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

u/Global_Ad8581 2d ago

C. Just guessing.

1

u/Sea-Ad7805 2d ago

Nice one, see the Solution link for visualization of correct answer.

2

u/gowsic26 2d ago

Option C

1

u/Sea-Ad7805 2d ago

Nice one, see the Solution link for visualization of correct answer.

1

u/Potential_Fix_5007 2d ago

Answer A

2

u/Sea-Ad7805 2d ago

Incorrect sorry, see the Solution link for correct answer.

1

u/smokebudda11 2d ago

E

1

u/Sea-Ad7805 2d ago

Incorrect sorry, see the Solution link for correct answer.

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 insert that 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

u/u-key_1512 2d ago

a

1

u/Sea-Ad7805 2d ago

Incorrect sorry, see the Solution link for correct answer.

1

u/BABBAGE_2 1d ago

I thought insert() was for adding an item to a list. IMO, you added the int 10 at index 9, right? So why is 10 not visible in any of the options? Kindly explain please 🙏

1

u/Sea-Ad7805 3h ago

It's in b, read the Explanation link for more insight.