Python Forum
Is there a way to append to a list WITHOUT modifying the parameter passed?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a way to append to a list WITHOUT modifying the parameter passed?
#1
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:

Output:
['one', 'two']
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.
Reply


Messages In This Thread
Is there a way to append to a list WITHOUT modifying the parameter passed? - by arnavb - Sep-14-2018, 11:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract args=, value that was passed to Multiprocessing.Proc object during runtime? haihal 1 614 Dec-08-2024, 07:04 AM
Last Post: Gribouillis
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,259 May-13-2024, 05:57 AM
Last Post: Larz60+
  append str to list in dataclass flash77 6 3,291 Mar-14-2024, 06:26 PM
Last Post: flash77
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 1,237 Feb-17-2024, 12:29 PM
Last Post: deanhystad
Question How to append integers from file to list? Milan 8 2,827 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 17,379 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  read a text file, find all integers, append to list oldtrafford 12 9,728 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 15,179 Jun-12-2022, 06:54 PM
Last Post: Mark17
  detecting a generstor passed to a funtion Skaperen 9 5,124 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 3,014 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020