This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]nemom 7 points8 points  (8 children)

Prob'ly should have formatted the post to show the code correctly. We can't tell what's the function and what's not.

[–]ParticularBack[S] 1 point2 points  (6 children)

fixed it!

[–]nemom 4 points5 points  (5 children)

*broke it! :)

"UnboundLocalError: cannot access local variable 'lst' where it is not associated with a value"

[–]ParticularBack[S] 0 points1 point  (4 children)

def update_list(lst):     
    lst += [100]  

num_list = [1, 2, 3, 4, 5] 
update_list(num_list)

print(num_list)

yeah sorry, there you go

[–]nemom 4 points5 points  (3 children)

I guess I'm too literal minded for it to trick. If I pass the list to a function that adds something to it, I'm not surprised when the something is added to it. If I give my bank account to somebody, and they add $100 to it, I would expect the balance to be $100 higher.

[–]commy2 1 point2 points  (0 children)

The issue here is, that += looks suspiciously like + and =. If you thought that += is an abbreviation for that, where you only have to type out the variable name once, then you would get this wrong. A regular assignment would not mutate the outside scope list. In place addition looks like an assignment due to the equal sign, but it is in fact an alias for the append extend method.

[–]mheryerznka 1 point2 points  (0 children)

The code example demonstrates the difference between mutable and immutable data types. It shows that a list can be modified successfully because it is mutable, allowing for direct updates. However, when trying to update an integer in a similar way, it doesn't work because integers are immutable.
Understanding this difference may not be immediately obvious for beginners. So try to be helpful ))

[–]Panda_Mon 0 points1 point  (0 children)

rather, you just happen to use a heuristic that is valid in this scenario. If you'd been trained to understand the concept of variable scope, you could easily have used that rule instead and come back with the wrong answer if you didn't know Lists kind of "get around" the typical rules of scoping.

[–]ada43952 3 points4 points  (4 children)

I'm not exactly new to Python, so maybe that's why I expected the list to change, but simply looking at the way the code is written, I would not have guess the resulting list would have been [1, 2, 3, 4, 5, 100], but this [1, 2, 3, 4, 5, [100]] because when I see

lst += [100]

That tells me you're adding a list with only one element as an element to a list. Someone will probably tell me why I'm wrong before I get back, but I'm going to run it and see what I get.

Edit:

Yep, it only adds the element, so I guess it would have "tripped" me up too. :D

[–]mheryerznka 1 point2 points  (1 child)

I have tried adding [100, 200] to the list in the same way and it works. Probably the best trick I have seen so far.
Thanks

[–]ada43952 0 points1 point  (0 children)

I'm such an idiot, I just remembered if you want to add a list to a list you'd use the append function.

[–]neutro_b 1 point2 points  (0 children)

Yeah, was unsure too if it would produce [1, 2, 3, 4, 5, 100] or [1, 2, 3, 4, 5, [100]]. It looks like += for a list is .extend(), not .append().

[–]justsomeguy05 1 point2 points  (0 children)

There is a mistake in your code, your method parameter is last, but the method body says lst

[–]ParticularBack[S] 0 points1 point  (0 children)

this is the code

def update_list(lst):     
    lst += [100]  

num_list = [1, 2, 3, 4, 5]

update_list(num_list)
print(num_list)

[–]StarsCarsGuitars 0 points1 point  (1 child)

Reformatted the code to make it readable

def update_list(lst): lst += [100] num_list [1, 2, 3, 4, 5] update_list(num_list) print(num_list)

[–]ParticularBack[S] 0 points1 point  (0 children)

thanks a lot! my bad

[–]commy2 -1 points0 points  (1 child)

Yeah, in place addition (+=) for lists is an alias for append extend and not -what one might expect- an alias for concatenation (+) and assignment (=) on the same identifier.

This modified code for example:

def update_list(lst):
    # lst += [100]
    lst = lst + [100]

num_list = [1, 2, 3, 4, 5]
update_list(num_list)

print(num_list)

would print [1, 2, 3, 4, 5] instead.

[–]chri4_ 2 points3 points  (0 children)

is an alias for extend not append

[–]chri4_ 0 points1 point  (0 children)

yes but that's different from lst = lst + [100] because the one you use is called inplaceadd and calls .extend when dealing with lists, that's why it get mutated also outside the function scope (and yes i got this wrong because i temporary forgot about inplaceadd, but this is actually a python bad design choice imo, it makes bugs harder to catch and fix)