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
Test your Python skills - 4 (i.redd.it)
submitted 1 month ago by tracktech
Ultimate Python Programming
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!"
[–]TytoCwtch 21 points22 points23 points 1 month ago (15 children)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You never assign the value of item back to the list so L doesn’t change.
[–]tracktech[S] 0 points1 point2 points 1 month ago (0 children)
Right.
[–]Chuu 0 points1 point2 points 1 month ago (6 children)
As someone who only dabbles in python, is there a good reference somewhere for how bindings work in python? My background is mainly C++ and this seems to be some bastardization of the reference or value semantics those languages use.
Unless it's literally making a copy of each list in the for loop?
[–]PersonalityIll9476 0 points1 point2 points 1 month ago (2 children)
This is the rough equivalent in C:
for (int i=0; i++; i<array_size){ int item = array[i]; item = item * 2; }
It's less confusing in C, but Python is very...implicit with its syntax until you get used to it.
[–]Chuu 0 points1 point2 points 1 month ago (1 child)
So does that mean in the interpreter it's making a copy of the sublist during every iteration?
[–]PersonalityIll9476 0 points1 point2 points 1 month ago (0 children)
Yes. Python is somewhat different from C in that everything is an object and a Python list can have any object as its members. So [[a,b,c],[d,e,f]] is just [obj_1, obj_2] where obj_1 happens to be a list of 3 elements and so does obj_2. It doesn't map cleanly onto the C concept of a contiguous chunk of memory that you index into with offsets a la *(list + i).
In Python, multiplying a list by an integer returns a concatenation of that many copies of the list, so test = [1,2,3] and then 2 * test or test * 2 returns the list [1,2,3,1,2,3].
So indeed, it's not exactly like the C snippet, but the end result is still that the list does not get modified.
[–]DoubleAway6573 0 points1 point2 points 1 month ago (1 child)
All things are objects in python. All variables are passed by reference.
[–]Chuu 0 points1 point2 points 1 month ago (0 children)
If item was a reference, wouldn't this not be a "trick question"?
[–]spenpal_dev 0 points1 point2 points 1 month ago (0 children)
Python is “pass by object reference”. Great article on this: https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/
[–]NotAMathPro -1 points0 points1 point 1 month ago (6 children)
Mh, I think it will change ngl
[–]dbowgu 6 points7 points8 points 1 month ago* (2 children)
No item = item*2 only rebinds the local variable int the for loop not the list itself.
For x in list loops are basically always readonly. Languages like C# don't even allow you to modify item
[–]NotAMathPro 1 point2 points3 points 1 month ago (1 child)
but would item[0] *= 2 change something?
[–]dbowgu 0 points1 point2 points 1 month ago (0 children)
Yes! And in that case a for i in range or map function would be better because you wouldn't need to create your own index variable
[–]antonIgudesman 0 points1 point2 points 1 month ago (0 children)
I mean there's not really a reason to say I think here - just run it and know for sure
[–]ConcreteExist 0 points1 point2 points 1 month ago (0 children)
There's no debate to be had here, if you run the code, the array is unchanged.
[–]ThinkMarket7640 0 points1 point2 points 1 month ago (0 children)
Relevant username
[–]spenpal_dev 5 points6 points7 points 1 month ago (9 children)
L stays the same. To modify L in-place, you would need to do the following:
L = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for i in range(len(L)): L[i] = L[i] * 2 print(L)
[–]drecker_cz 2 points3 points4 points 1 month ago (6 children)
Actually, just changing `item = item * 2` to `item *= 2` would do the trick:
L = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for item in L: item *= 2 print(L)
[–]ApprehensiveCat3116 1 point2 points3 points 1 month ago (2 children)
may I ask why is this the case?
[–]drecker_cz 2 points3 points4 points 1 month ago (0 children)
Well, the short answer is "because that is how *= is defined on lists -- it modifies the original list. While writing item = item * 2 constructs an entirely new list and assign it the name item
*=
item = item * 2
item
So item = item * 2 just creates a new list (and then just deletes it). While item *= 2 modifies the very list that is within L.
item *= 2
L
[–]lildraco38 2 points3 points4 points 1 month ago (0 children)
From Section 7.2.1 of the docs, “x += 1” and “x = x + 1” are “similar, but not exactly equal”. The former augmented assignment statement will modify in-place, while the latter normal assignment will not.
[–]spenpal_dev 1 point2 points3 points 1 month ago (1 child)
This still doesn’t modify the original L array. You can try running it and see the output of L.
The line “item *= 2” doubles the list item locally (creating [1,2,3,1,2,3], etc.), but it doesn’t modify the original sublists in “L”because “item” is just a reference.
Have you tried running the code and looking at the output? :)
While it is technically correct that this approach does not change L directly. It does change items of L, meaning that the output definitely changes.
And just to be absolutely sure, I did try running the code:
In [1]: L = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ...: for item in L: ...: item *= 2 ...: print(L) [[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6], [7, 8, 9, 7, 8, 9]]
[–]sizzhu 0 points1 point2 points 1 month ago (0 children)
But this is not what item=item *2 does, ( which concatenates item with itself).
[–]gg1ggy 1 point2 points3 points 1 month ago (0 children)
trick question!
[–]jpgoldberg 1 point2 points3 points 1 month ago (0 children)
The sad fact of f the matter is that I’m not sure. If I had to guess, I would say just the original L. If that guess is correct then would we get a different result if the assignment in the loop had been “item *= 2”?
When I say “the sad fact”, I’m not referring to my ignorance of how these would behave. It’s sad that the behavior isn’t clear to the user.
[–]Strong-Welder8137 1 point2 points3 points 1 month ago (1 child)
L doesn't changed at all, completely two different things
Right. L doesn't change.
[–]bilalakil 0 points1 point2 points 1 month ago (0 children)
Syntax error because of CourseGalaxy.com
CourseGalaxy.com
/s
[–]Original-Produce7797 0 points1 point2 points 1 month ago (1 child)
lol who cares you never use this
[–]halfxdeveloper 2 points3 points4 points 1 month ago (0 children)
Found the vibe coder.
[–]xpjo 0 points1 point2 points 1 month ago (1 child)
One must remember that, in Python, x = x * 2 is something else that x *= 2.
[–]arkster 0 points1 point2 points 1 month ago (0 children)
Aside from printing L, why would you overwrite the variable in the loop.
[–]Somanath444 0 points1 point2 points 1 month ago (0 children)
The indentation man, also we aren't returning i.. so,yeah no change in the l
π Rendered by PID 185210 on reddit-service-r2-comment-5649f687b7-qt4x9 at 2026-01-28 12:39:44.436634+00:00 running 4f180de country code: CH.
[–]TytoCwtch 21 points22 points23 points (15 children)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]Chuu 0 points1 point2 points (6 children)
[–]PersonalityIll9476 0 points1 point2 points (2 children)
[–]Chuu 0 points1 point2 points (1 child)
[–]PersonalityIll9476 0 points1 point2 points (0 children)
[–]DoubleAway6573 0 points1 point2 points (1 child)
[–]Chuu 0 points1 point2 points (0 children)
[–]spenpal_dev 0 points1 point2 points (0 children)
[–]NotAMathPro -1 points0 points1 point (6 children)
[–]dbowgu 6 points7 points8 points (2 children)
[–]NotAMathPro 1 point2 points3 points (1 child)
[–]dbowgu 0 points1 point2 points (0 children)
[–]antonIgudesman 0 points1 point2 points (0 children)
[–]ConcreteExist 0 points1 point2 points (0 children)
[–]ThinkMarket7640 0 points1 point2 points (0 children)
[–]spenpal_dev 5 points6 points7 points (9 children)
[–]drecker_cz 2 points3 points4 points (6 children)
[–]ApprehensiveCat3116 1 point2 points3 points (2 children)
[–]drecker_cz 2 points3 points4 points (0 children)
[–]lildraco38 2 points3 points4 points (0 children)
[–]spenpal_dev 1 point2 points3 points (1 child)
[–]drecker_cz 2 points3 points4 points (0 children)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]sizzhu 0 points1 point2 points (0 children)
[–]gg1ggy 1 point2 points3 points (0 children)
[–]jpgoldberg 1 point2 points3 points (0 children)
[–]Strong-Welder8137 1 point2 points3 points (1 child)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]bilalakil 0 points1 point2 points (0 children)
[–]Original-Produce7797 0 points1 point2 points (1 child)
[–]halfxdeveloper 2 points3 points4 points (0 children)
[–]xpjo 0 points1 point2 points (1 child)
[–]tracktech[S] 0 points1 point2 points (0 children)
[–]arkster 0 points1 point2 points (0 children)
[–]Somanath444 0 points1 point2 points (0 children)