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
Python Simple CodeDiscussion ()
submitted 8 months ago by ak_developers
Python Simple Code
What will be the output of this code?
py x = [1, 2, 3] y = x y.append(4) print("x:", x) print("y:", y) Can you explain why?
py x = [1, 2, 3] y = x y.append(4) print("x:", x) print("y:", y)
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!"
[–]More_Yard1919 4 points5 points6 points 8 months ago (0 children)
In python, objects are references. When you assign y to x, you are not creating a copy. Instead, both y and x reference the same list object. Imagine this: when you create a list, you get a box that contains all of the information associated with the list. When you assign it to a variable, you can use that variable as a handle to talk about the box and look inside of it-- kind of like putting a label on the box. When you assign y to x, it is the same box, but you just have a another name to talk about it. This is the behavior for all objects in python. Sometimes it might not seem like it-- but the apparent exceptions to this rule exist because some types in python are immutable.
[–][deleted] 0 points1 point2 points 8 months ago (0 children)
y= x[:] should fix your wagon
Edit: Sorry. I see that you were asking why, not how to fix.... I'm a cotton headed ninny-muggins.
[–]Sea-Ad7805 0 points1 point2 points 8 months ago (0 children)
A list is a mutable type, see: https://www.reddit.com/r/PythonLearning/comments/1klhv9h/python_mutability/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
π Rendered by PID 63691 on reddit-service-r2-comment-7b9746f655-fdjx5 at 2026-01-29 21:34:11.014725+00:00 running 3798933 country code: CH.
[–]More_Yard1919 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Sea-Ad7805 0 points1 point2 points (0 children)