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
#2
what is the desired output?
Reply
#3
By adding lists - yes you can

Output:
In [4]: list_ = [1] In [5]: list_1 = list_ + [2] In [6]: list_ Out[6]: [1]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
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.
Reply
#5
Possible since 3.5 I think:

def test(some_args):
    return [*some_args, 'two']
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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
#7
If you don't want to modify the original why not tuple?

>>> t = ('one',)
>>> t += ('two',)
>>> t
('one', 'two')
>>> 
Hm! This actually modifies it Confused
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Sep-17-2018, 10:08 AM)wavic Wrote: If you don't want to modify the original why not tuple?

>>> t = ('one',)
>>> t += ('two',)
>>> t
('one', 'two')
>>> 
Hm! This actually modifies it Confused

No, it creates a new t object

Output:
In [28]: t = ('one',) In [29]: id(t) Out[29]: 140190907161512 In [30]: >>> t += ('two',) In [31]: id(t) Out[31]: 140190903830408
With lists, += will be equivalent to append (or extend)
Output:
In [34]: id(t) Out[34]: 140190903120520 In [35]: t = [1] In [36]: id(t) Out[36]: 140190903091912 In [37]: t += [2] In [38]: id(t) Out[38]: 140190903091912 In [39]: t Out[39]: [1, 2]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
You are right. I missed that I used = sign so I overwitted the object. Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Sep-17-2018, 11:11 AM)wavic Wrote: You are right. I missed that I used = sign so I overwitted the object. Smile
how witty! Big Grin
Tradition is peer pressure from dead people

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


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