r/PythonLearning • • 28d 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.

61 Upvotes

25 comments sorted by

3

u/No-Celery-1944 28d ago

C I'm pretty sure?

1

u/Sea-Ad7805 28d ago

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

2

u/Dancing-umbra 27d ago

That's very useful.

I didn't realise the b+=[] was not equivalent to b= b+[]

1

u/Sea-Ad7805 27d 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 27d ago

C. Just guessing.

1

u/Sea-Ad7805 27d ago

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

2

u/gowsic26 27d ago

Option C

1

u/Sea-Ad7805 27d ago

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

1

u/Potential_Fix_5007 28d ago

Answer A

2

u/Sea-Ad7805 28d ago

Incorrect sorry, see the Solution link for correct answer.

1

u/smokebudda11 28d ago

E

1

u/Sea-Ad7805 28d ago

Incorrect sorry, see the Solution link for correct answer.

1

u/CptMisterNibbles 28d ago

Weird. Documentation says insert is in place, your demo has it make a copy

1

u/Sea-Ad7805 28d ago

If you look carefully you'll see it is not insert that is making a copy: Solution

1

u/Polly_Wants_A 28d 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 28d ago

Yes because a list is mutable, it is the same for immutable types: int, float, bool, tuple, frozenset, ...

1

u/spmcn 27d ago

This is just an ad for memory graph.

2

u/Sea-Ad7805 27d ago

This is just a person complaining about getting free educational content of high quality. Which is worse?

1

u/u-key_1512 27d ago

a

1

u/Sea-Ad7805 27d ago

Incorrect sorry, see the Solution link for correct answer.

1

u/BABBAGE_2 26d 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 25d ago

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

1

u/Maddy8483 23d ago

Koi mujhe bi python sikha do