Is there a way to append to a list WITHOUT modifying the parameter passed? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Is there a way to append to a list WITHOUT modifying the parameter passed? (/thread-12828.html) Pages:
1
2
|
Is there a way to append to a list WITHOUT modifying the parameter passed? - arnavb - Sep-14-2018 Let's say I have this code: def test(some_args): some_args += ['two'] args = ['one'] test(args) print(args)When I run this code, the output is: Is there a way to append to a list WITHOUT an in-place assignment which modifies the parameter itself? I've tried .append('two'), but that doesn't work either. I realize that I can just make a copy of the list using .copy() or with slice notation, but I want to avoid creating an extra variable just for this purpose.
RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - Larz60+ - Sep-14-2018 what is the desired output? RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - volcano63 - Sep-15-2018 By adding lists - yes you can
RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - arnavb - Sep-16-2018 Yes, but this requires creating another list for the purpose. Basically, I'm wondering if there is any way to emulate pass-by-value behavior somehow in Python. RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - DeaD_EyE - Sep-16-2018 Possible since 3.5 I think: def test(some_args): return [*some_args, 'two'] RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - Skaperen - Sep-17-2018 so you want to append to the list and have the function see the effect but have the caller not see it. since the function gets the object (the list) using pass-by-reference the only real way to have that effect is to make a copy of the list. if there are no lists within the list then a shallow copy is sufficient. you can do this in the call like test(args[:]) or you do this in the function by assigning a copy to the variable it will be using below.Python is a language that can do almost anything. it just doesn't always do it the way you want or expect. RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - wavic - Sep-17-2018 If you don't want to modify the original why not tuple? >>> t = ('one',) >>> t += ('two',) >>> t ('one', 'two') >>>Hm! This actually modifies it RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - volcano63 - Sep-17-2018 (Sep-17-2018, 10:08 AM)wavic Wrote: If you don't want to modify the original why not tuple? No, it creates a new t object With lists, += will be equivalent to append (or extend )
RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - wavic - Sep-17-2018 You are right. I missed that I used = sign so I overwitted the object. RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - Skaperen - Sep-19-2018 (Sep-17-2018, 11:11 AM)wavic Wrote: You are right. I missed that I used = sign so I overwitted the object.how witty! |