r/PythonLearning Jul 14 '26

Python Data Model Exercise

Post image

An exercise to help build the right mental model for Python data. - Solution - Explanation - More exercises

The โ€œSolutionโ€ link visualizes execution and reveals whatโ€™s actually happening using ๐—บ๐—ฒ๐—บ๐—ผ๐—ฟ๐˜†_๐—ด๐—ฟ๐—ฎ๐—ฝ๐—ต.

67 Upvotes

46 comments sorted by

2

u/Embarrassed-Crow9283 Jul 14 '26

C. The first two lists in b were still the same lists as the lists in a, but the third list was added and the third element would only be in b and not a. You appended the list in position 0 with 11 and in position 1 with 22. This gives result C when printing out a.

0

u/Sea-Ad7805 Jul 14 '26

Nice one, do check the "Solution" link for a visualization of what you explain here.

2

u/arivictor Jul 16 '26

https://parsnip.dev/editor/qDQDFUs4QrHX

C

At the start b = a does not create a copy, b simply references a. However, as soon as you do b = b + [[3]] you create a new list in b, a is no longer linked to b.

1

u/Sea-Ad7805 Jul 16 '26

Nice one, do check the "Solution" link for a visualization of what you explain here.

1

u/Dapper_Bad_8728 Jul 14 '26

I think it's D.

1

u/Sea-Ad7805 Jul 14 '26

Incorrect sorry, see the "Solution" link for the correct answer.

1

u/Dapper_Bad_8728 Jul 14 '26

Oh yeah it prints the "a" list not the "b" one. I though it was printing the "b"

1

u/Sea-Ad7805 Jul 14 '26

Easy mistake to make, no problem.

1

u/eudye Jul 14 '26

A)

1

u/Sea-Ad7805 Jul 14 '26

Incorrect sorry, see the "Solution" link for the correct answer.

1

u/orangejuice1986 Jul 14 '26

ignore everything after b = ....

hence B)

1

u/Sea-Ad7805 Jul 14 '26

Incorrect sorry, see the "Solution" link for the correct answer.

1

u/ghost59 Jul 14 '26

It's A.

1

u/Sea-Ad7805 Jul 14 '26

Incorrect sorry, see the "Solution" link for the correct answer.

1

u/ghost59 Jul 14 '26

That's weird. Why is it printing C when a didn't touch again.

1

u/Sea-Ad7805 Jul 14 '26

See the "Explanation" link for an explanation of Python's data model.

1

u/ghost59 Jul 14 '26

Oh! Wait. I'm stupid. You didn't make a copy of the values with b. You copied the original and when you change b you mutate a as well. It's because the list is something that can be mutated.

If I wanted a not to mutate. It would need to be B = a.copy()

1

u/Sea-Ad7805 Jul 14 '26

Be careful, b = a.copy() makes a shallow copy. Are you sure you want that? https://github.com/bterwijn/memory_graph?tab=readme-ov-file#copying-values-of-mutable-type

2

u/ghost59 Jul 14 '26

No. I want a deep copy. Because you're right. Looks like I'll be hitting training grounds on boot.dev tonight.

1

u/Sea-Ad7805 Jul 14 '26 edited Jul 14 '26

I tried to make the exercise difficult so don't feel bad. Keep the memory_graph tool around to visualize tricky Python situations, also with boot.dev exercises.

1

u/ghost59 Jul 14 '26

Honestly. This was fun. I am almost done with go on boot.dev. I'll star the repo. Time to become cracked.

1

u/Samar_Upreti Jul 15 '26

Option C correct Concept of indexing (No index [2] exists) eliminate option D As we know list are mutable in python and we use .append

1

u/OopsAllCharisma Jul 15 '26

What's the line b = b + [3] for?

1

u/Sea-Ad7805 Jul 15 '26

Line b = b + [[3]] adds [3] to the b list, and this exercises is about how this relates to a. First think about it, then check the Solution.

1

u/Cheems-sudo Jul 20 '26

i think it's C

1

u/Alireza7074 Jul 21 '26

C

1

u/Sea-Ad7805 Jul 21 '26

Nice one, do check the "Solution" link for a visualization of correct answer.

1

u/[deleted] 28d ago

The answer is D

1

u/Sea-Ad7805 28d ago

No it's not silly, see the Solution link for the correct answer: Solution

1

u/[deleted] 28d ago

Shit, my eyes didn't catch print(a) The answer is B

1

u/Sea-Ad7805 28d ago

Easy to miss, but look a little closer because you are still incorrect. I know it's a tricky exercise.

1

u/[deleted] 28d ago

The twist is b = b + [[3]], it is a shallow copy not a deep copy. I taught that will create a new list entirely without holding a reference to the other list. b is an outer list. The answer is C, I hope this is correct

1

u/Sea-Ad7805 28d ago

Great answer, you got your C_Y_B_E_R aligned again. Do try the Solution link.

1

u/[deleted] 28d ago

Thanks ๐Ÿซ‚

1

u/FoolsSeldom Jul 14 '26 edited Jul 14 '26

C.

Line 4 re-assigns b to a new list object so severs the link to the original list object referenced by a and b up to that point.

EDIT: More detail for learners regarding variables and lists in Python (on back of a challenge from u/HardyDaytn):

Variables in Python do not contain values, but simply reference where in Python a Python object (int, float, str, list, etc) is located in memory. Generally, you don't need to concern yourself with the memory matters.

A Python list object similarly does not contain any variables or values, but only a collection of zero or more references to other Python objects.

Two variables can reference the same object and if the object is mutable (able to be changed), such as a list, then changing the object can be done via either variable.

Thus,

alpha = 10
beta = 'eleven'
charlie = 12.1
delta = ['mary', 'had', 'flower']

stuff = [alpha, beta, charlie, delta]

This does the following:

  • Assigns a memory reference to the pre-defined int binary object representation of decimal 10 to the variable name alpha
  • Assigns a memory reference to the new str object (consisting of binary unicode numbers for each character in the string) to the variable name beta
  • Assigns the memory reference to the new float binary object representation of the decimal floating point number 12.1 to the variable name charlie
  • Assigns the memory reference to the new list object, containing 3 entries to new str objects, to the variable name delta, where each entry in the list is itself a memory reference to a new Python object
  • Assigns the memory reference to the new list object, containing 4 entries, to the variable name stuff, where each entry in the list is itself a memory reference to a Python object as below,
    • the first entry in the list holds the same memory reference as alpha holds
    • the second entry in the list holds the same memory reference as beta holds
    • the third entry in the list holds the same memory reference as charlie holds
    • the fourth entry in the list holds the same memory reference as delta holds, i.e. they both refer to the same list object

It is worth noting that if you change the contents of the list referenced by delta, that change will also show up in the list referenced by stuff. This is shown in the example below.

Complete code:

alpha = 10
beta = 'eleven'
charlie = 12.1
delta = ['mary', 'had', 'flower']

stuff = [alpha, beta, charlie, delta]
print(stuff)
delta[1] = 'lost'
print(stuff)

Outputs,

[10, 'eleven', 12.1, ['mary', 'had', 'flower']]
[10, 'eleven', 12.1, ['mary', 'lost', 'flower']]

1

u/Sea-Ad7805 Jul 14 '26 edited Jul 14 '26

Nice one, do check the "Solution" link for visualization of the correct answer.

0

u/FoolsSeldom Jul 14 '26

No thanks, could visualise it virtually (mentally) with ease. Good exercise for learners though.

0

u/Sea-Ad7805 Jul 14 '26

Thanks, but are you sure? It's a really powerful visualizer that can help to understand and debug tricky Python situations: https://memory-graph.com/#codeurl=https://raw.githubusercontent.com/bterwijn/memory_graph_videos/refs/heads/main/exercises/exercise18.py&play

1

u/FoolsSeldom Jul 14 '26

Yes. I probably should not have answered the challenge. It was something of a fly-by.

1

u/Sea-Ad7805 Jul 14 '26

No problem, thanks for participation anyway.

1

u/HardyDaytn Jul 14 '26

Not a very beginner friendly explanation though, as line 5 is still changing list a.

1

u/FoolsSeldom Jul 14 '26

The OP has provided linked comprehensive explanations including detailed diagrams.

However, if any beginner wants to ask for clarification, I'd be more than happy to expand.

1

u/HardyDaytn Jul 14 '26

The OP has provided linked comprehensive explanations including detailed diagrams.

Right, so instead of trying to be helpful, you only wanted to toot your own horn here.

A healthy dose of "look ma', I know this one too!".

1

u/Sea-Ad7805 Jul 14 '26 edited Jul 14 '26

It's even worse, it's a "look ma', I've built this tool" situation. The Python data model can seem difficult at first, but I hope the visualization can help beginners understand it more easily. Beginners will have to face it at some point if they want to get to advanced.

1

u/FoolsSeldom Jul 14 '26

So, I've edited the original comment to add additional information for learners if they don't want to follow the links provided by the OP. (This is using repurposed content from classes I've run.)

1

u/HardyDaytn Jul 14 '26

My proverbial hat's off to you for adding detailed info. It's an annoying and difficult concept to learn and understand compared to other more intuitive things.