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?
#6
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
RE: Is there a way to append to a list WITHOUT modifying the parameter passed? - by Skaperen - Sep-17-2018, 12:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 576 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 381 Feb-17-2024, 12:29 PM
Last Post: deanhystad
Question How to append integers from file to list? Milan 8 1,509 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,232 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  read a text file, find all integers, append to list oldtrafford 12 3,713 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 10,788 Jun-12-2022, 06:54 PM
Last Post: Mark17
  Modifying code cheburashka 1 1,331 Dec-13-2021, 01:01 PM
Last Post: Kebap
  detecting a generstor passed to a funtion Skaperen 9 3,722 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,367 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  how to modify a variable that is passed as parameter StefanL38 2 2,145 Dec-07-2020, 08:39 AM
Last Post: StefanL38

Forum Jump:

User Panel Messages

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