Everything is a reference in Python. If you wish to avoid that behavior you would have to create a new copy of the original with
list(). If the list contains more references, you’d need to use deepcopy()
def modify(l): l.append('HI') return l def preserve(l): t = list(l) t.append('HI') return t example = list() modify(example) print(example) example = list() preserve(example) print(example)
- How can I return two values from a function in Python?
- Apply function to each element of a list
- Why does using `arg=None` fix Python’s mutable default argument issue?
- Function changes list values and not variable values in Python [duplicate]
- How do I create a Python function with optional arguments?
- Python: Passing variables between functions
- Best way to check function arguments? [closed]
- Overloaded functions in Python
- Python function as a function argument?
- How do I define a function with optional arguments?
- Python: return the index of the first element of a list which makes a passed function true
- How does using a function (callback) as an argument to another function work in Python?
- How to apply a function to each sublist of a list in python?
- Get a list/tuple/dict of the arguments passed to a function?
- Python Class Based Decorator with parameters that can decorate a method or a function
- How to have multiple conditions for one if statement in python [duplicate]
- Converting list to *args when calling function [duplicate]
- Check if all values in list are greater than a certain number
- How do I check if a list is empty?
- What are the advantages of NumPy over regular Python lists?
- How do I initialize a dictionary of empty lists in Python?
- Get unique values from a list in python [duplicate]
- Why can’t I use a list as a dict key in python?
- Extract first item of each sublist
- Negative list index? [duplicate]
- Python recursion with list returns None [duplicate]
- Why can tuples contain mutable items?
- How do I check if there are duplicates in a flat list?
- How to expand a list to function arguments in Python [duplicate]
- Call a function with argument list in python
- How to perform element-wise multiplication of two lists?
- Calculating arithmetic mean (one type of average) in Python [duplicate]
- Defining private module functions in python
- Looping over a list in Python [closed]
- How do I split a string into a list?
- Interleaving Lists in Python [duplicate]
- Remove dictionary from list
- How to compare a list of lists/sets in python?
- Convert list into a dictionary [duplicate]
- Modifying a list while iterating over it – why not? [duplicate]
- Python: filter list of list with another list
- How to convert list of tuples to multiple lists?
- Identify duplicate values in a list in Python
- Find min, max, and average of a list
- Why does list ask about __len__?
- Co-occurrence matrix from nested list of words
- Joining elements in a list without the join command
- Union find implementation using Python
- Tkinter PIL image not displaying inside of a function
- Converting string to tuple without splitting characters