This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm trying to understand the difference between the two methods below:
def first_append(new_item, a_list=[]):
a_list.append(new_item)
return a_list
def second_append(new_item, a_list=None):
if a_list is None:
a_list = []
a_list.append(new_item)
return a_list
first_append keeps adding to a_list when called multiple times, causing it to grow. However, second_append always returns a list of length 1. What is the difference here?
Examples:
>>> first_append('one')
['one']
>>> first_append('two')
['one', 'two']
>>> second_append('one')
['one']
>>> second_append('two')
['two']
Function second_append always creates a new local list for you each time it is called.
Related
This question already has answers here:
Return statement in method that modifies in-place
(2 answers)
Should I ever return a list that was passed by reference and modified?
(4 answers)
Pythonic way to write a function which modifies a list?
(2 answers)
Closed 8 months ago.
Consider a function in Python that gets as one of its arguments a list and inside it we append something to it. For instance:
def add_new_number_squared(new_number, numbers_list):
numbers_list.append(num**2)
my_list = [0]
add_new_number_squared(2, my_list)
Now my_list[1] will contain the value 4, but is it a good practice or should I return numbers_list inside add_new_number_squared instead? To be more clear, should I write:
def add_new_number_squared(new_number, numbers_list):
numbers_list.append(new_number**2)
return numbers_list
my_list = [0]
my_list = add_new_number_squared(2, my_list)
instead of the code above?
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 2 years ago.
Python beginner here. Confused about the nature of list assignment.
a = [1, 2, 3]
b = a.reverse()
>>>a
>>>[3,2,1]
>>>b
>>> # nothing shows up
or
e = [1,2,3]
f = e.append(4)
>>>e
>>>[1,2,3,4]
>>>f
>>> # nothing shows up
Why does assigning to b or f not work here.
I believe it has to do with the mutability of lists? Am i completely wrong? Thanks
Both these methods do an in-place modification, i.e.:
b = a.reverse()
f = e.append(4)
They modify the original object and do not create a new one. Hence, you see None when you try and print these.
As per #juanpa.arrivillaga:
Note: The fact that these methods return None is a convention.
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 5 years ago.
Look at the code:
a = ['Hello']
b = a # ['Hello']
b.append(2)
print b # ['Hello', 2]
print a # ['Hello', 2]
Here, a is assigned to b, meaning that the change of value in a can affect the value of b. How can the change in b can affect a in this case?
Is it that List in python have any special rule where appending a value can affect both a and b?
Because it's exactly the same value. Names are bound to references, which means that b = a points both to the same list.
This question already has answers here:
Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?
(5 answers)
Closed 5 years ago.
The outcome is None with list(a) the second time. Anyone have a clue on that?
>>> test = {1: 2, 3: 4}
>>> a= test.iterkeys()
>>> list(a)
**[1, 3]**
>>> list(a)
**[]**
>>> list(a)
[]
iterkeys returns an iterator, which, as any iterator, can be iterated over only once.
list consumes the whole iterator, so that the latter can't provide any more values, so the subsequent lists are empty.
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 6 years ago.
I noticed something odd demonstrated below:
>>> print [].append(1)
None
>>> a = [].append(1)
>>> print a
None
>>> a = []
>>> a.append(1)
>>> print a
[1]
I do not understand what the difference between the first two statements and the last one is. Can anyone explain?
EDIT:
This question was asked poorly. I should also have noted that:
>>> print [] + [1]
[1]
Thank you for explaining that the return value for operations on mutable data-types in python is normally None.
The .append() method does not return a modified list as you would expect.
With this line a = [].append(1) you're assigning the return value (which is None in Python by default) to the variable a. You're not assigning an array that you've just appended to.
However, with this code
>>> a = []
>>> a.append(1)
you're modifying the array in the variable a.