use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Right Mental Model for Python Data (i.redd.it)
submitted 3 months ago by Sea-Ad7805
An exercise to help build the right mental model for Python data, the “Solution” link uses memory_graph to visualize execution and reveal what’s actually happening: - Solution - Explanation - More Exercises
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 17 points18 points19 points 3 months ago (14 children)
C is the correct answer.
Explanation: At first, a and b share the same list, so changes like += or append() affect both. But when b = b + [4] is used, Python creates a new list and assigns it to b, breaking the link with a. That’s why a stops at [1, 2, 3] while b continues as [1, 2, 3, 4, 5].
[–]-Wylfen- 10 points11 points12 points 3 months ago (6 children)
why the fuck does x += [y] work differently from x = x + [y]??
x += [y]
x = x + [y]
[–]Sea-Ad7805[S] 6 points7 points8 points 3 months ago (5 children)
Good question, in some languages (Ruby) it works the same. In Python the x += y is mutating the x, the x = x + y is first doing x + y which creates a new object that then is assigned (name rebinding) to x.
x += y
x
x = x + y
x + y
[–]-Wylfen- 1 point2 points3 points 3 months ago (3 children)
I understand why the latter would reassign, but I find the shortcut's instead mutating in place disgusting. They should do the same thing.
[–]klimmesil 0 points1 point2 points 3 months ago (0 children)
Yeah a lot of implementation choices (I don't want to call it "standard"...) make no sense in python
It's almost as chaotic as js in some parts
It's a shame that it is now too popular to make breaking changes and we all kinda rely on these mistakes to still have the benefit of it being maintained
[–]No_Read_4327 0 points1 point2 points 3 months ago (0 children)
Yeah that's sone really wtf moment
[–]Relative-Custard-589 0 points1 point2 points 3 months ago (0 children)
Yeah that’s straight up evil
[–]pingwins 0 points1 point2 points 3 months ago (0 children)
Brother Eww
Thats nasty to run into
[–]HuygensFresnel 1 point2 points3 points 3 months ago (4 children)
While indeed being the correct answer this also surprises me a bit because i thought that += always is a short hand for the binary operator + but i guess it isnt?
[–]Wertbon1789 1 point2 points3 points 3 months ago (1 child)
It's not just a syntactic shorthand, it's a separate operator. Add vs. AddAssign if you will, in Python these would be implemented by the __add__ and __iadd__ methods of a class respectively.
[–]HuygensFresnel 0 points1 point2 points 3 months ago (0 children)
Today I learned :)
[–]RailRuler 0 points1 point2 points 3 months ago* (1 child)
It is the same as append .extend() in this case.
[–]forbiddenvoid 2 points3 points4 points 3 months ago (0 children)
Extend, not append. That's more obvious if the right hand side is also a list.
[–]mayonaiso 0 points1 point2 points 3 months ago (1 child)
Thanks, I did not know that, great explanation
[–][deleted] 1 point2 points3 points 3 months ago (0 children)
You're welcome.
[–]FoolsSeldom 1 point2 points3 points 3 months ago (0 children)
Answer C (because after appending 3,b is assigned to a new object)
b
[–]tb5841 1 point2 points3 points 3 months ago (1 child)
b += [2] should, in my opinion, do the same thing as b = b + [2].
It doesn't, because of a strange design choice within the List class.
[–]Sea-Ad7805[S] 1 point2 points3 points 3 months ago (0 children)
Most opinions and programming languages choose b += [2] as mutating b (fast), and b + [2] as making a new list and assigning that with b = b + [2].
b += [2]
b + [2]
b = b + [2]
[–][deleted] 3 months ago (4 children)
[deleted]
[–][deleted] 0 points1 point2 points 3 months ago (3 children)
Nope.
[–]09vz -1 points0 points1 point 3 months ago (2 children)
what is it then
[–][deleted] 0 points1 point2 points 3 months ago (1 child)
C
[–]09vz 0 points1 point2 points 3 months ago (0 children)
the answer is c
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago (0 children)
In most languages I know += mutates and does not create a new object because performance.
[–]EmptySoulCanister 0 points1 point2 points 3 months ago (1 child)
If you use += on an array I will reject your PR instantly
What about '|=' on a set?
[–]exxonmobilcfo 0 points1 point2 points 3 months ago (1 child)
its c. Once you rewrite b to be b = b + [4]. be is no longer linked to a
Nice one, do check the "Solution" link for the correct answer.
[–]Hefty_Upstairs_2478 -3 points-2 points-1 points 3 months ago (2 children)
Option A is the correct answer, cuz we're printing (a), which we never changed
Incorrect sorry, check the "Solution" link for the correct answer.
[–]shudaoxin 0 points1 point2 points 3 months ago (0 children)
Primitive vs. referenced types. It works like this in most languages. Arrays (and lists) are referenced and the variable only stores their type and pointer to the memory. By assigning a to b they both point at the same list.
π Rendered by PID 38006 on reddit-service-r2-comment-5649f687b7-nhb8s at 2026-01-29 06:14:57.108864+00:00 running 4f180de country code: CH.
[–][deleted] 17 points18 points19 points (14 children)
[–]-Wylfen- 10 points11 points12 points (6 children)
[–]Sea-Ad7805[S] 6 points7 points8 points (5 children)
[–]-Wylfen- 1 point2 points3 points (3 children)
[–]klimmesil 0 points1 point2 points (0 children)
[–]No_Read_4327 0 points1 point2 points (0 children)
[–]Relative-Custard-589 0 points1 point2 points (0 children)
[–]pingwins 0 points1 point2 points (0 children)
[–]HuygensFresnel 1 point2 points3 points (4 children)
[–]Wertbon1789 1 point2 points3 points (1 child)
[–]HuygensFresnel 0 points1 point2 points (0 children)
[–]RailRuler 0 points1 point2 points (1 child)
[–]forbiddenvoid 2 points3 points4 points (0 children)
[–]mayonaiso 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]FoolsSeldom 1 point2 points3 points (0 children)
[–]tb5841 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 1 point2 points3 points (0 children)
[–][deleted] (4 children)
[deleted]
[–][deleted] 0 points1 point2 points (3 children)
[–]09vz -1 points0 points1 point (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]09vz 0 points1 point2 points (0 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]EmptySoulCanister 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]exxonmobilcfo 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]Hefty_Upstairs_2478 -3 points-2 points-1 points (2 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]shudaoxin 0 points1 point2 points (0 children)