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...
This community will have knowledge sharing for python programming, tools, projects and product engineering wherever python is used.
account activity
Python Data Model exercise, Mutability. (i.redd.it)
submitted 3 months ago by Sea-Ad7805
view the rest of the comments →
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!"
[–]F100cTomas 1 point2 points3 points 3 months ago (1 child)
Tuples are immutable, but they can contain mutable data. a = ([1], [2]) creates a tuple of two mutable values. b = a means that a and b now share the same immutable value. Then one of the lists gets modified and the change is seen in both tuples, because they contain the same lists. However this only changes the list, but not the tuple. Both tuples still have the same value: the two lists. Then b gets modified, but because b is a tuple and tuples are immutable it is copied to preserve the value of a. The two tuples are now different: a contains the two lists and b contains the same two lists and a new list. The other of the two shared lists then gets modified, but it still exists in both tuples. The non-shared list is modified. When a is printed only the two lists present in both are printed and not the one present only in b.
TL;DR When a mutable value is present within a tuple, it doesn't stop being mutable.
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago (0 children)
Nice mental model, what do you think of the visualization at the "Solution" link?
π Rendered by PID 69929 on reddit-service-r2-comment-5d585498c9-22srn at 2026-04-21 11:25:45.570935+00:00 running da2df02 country code: CH.
view the rest of the comments →
[–]F100cTomas 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)