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.
Related
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:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 4 years ago.
I'm very confused about a situation. Why I cannot assign a list to a variable. Can someone explain it to me?
a =[1,2,3,4]
b = a.insert(0,1)
print(b)
the output is
None
The insert() method only inserts the element to the list. It doesn't return any value.
Adding to the replies that you got, if it is the list b that needs to be updated but not the list a, you should proceed as such:
a =[1,2,3]
b = list(a)
b.insert(1,20)
print(a, b)
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 5 years ago.
aTup = (1,2,3)
print(list(aTup).append(4))
Why does it display None?
append returns None, simple as that. It does however modify the list
>>> l = [1,2,3]
>>> print(l.append(4))
None
>>> l
[1, 2, 3, 4]
The reason is that it isn't meant to be called with the return value used mistakenly for an assignment.
l = l.append(4) # this is wrong
It's because the method append() does not return any value.
If you want to print the list after the update, you can do the following:
aTup = (1,2,3)
aList = list(aTup)
aList.append(4)
print(aList)
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.
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 7 years ago.
I am new to python programming
why this assignment value to k is giving 'None' value
> >>> l=[2,3,4]
> >>> k=l.append(14)
> >>> print k
None
> >>> print l
[2, 3, 4, 14]
in above example List,I append the 14 value to list and then assigned to k but k is printing None please tell me the reason why its printing None instead of appended list?
Thanks
mukthyar
append changes the current list and doesn't return anything. Use:
k = l + [14]
or
k = l[:] # Copy list
k.append(14)
Most methods in Python which mutate their instance (.append(), .sort(), et al.) do not return a copy of the object. Thus l.append(X) does actually return "None" just as l.sort() would.
You'd probably want to use something more like:
l.append(14)
k = l[-1]
In python default return type is None so if your function is not return any value then you will get the None from that function. There are different functions for the object. Some functions are perform on same object and others are return the modified copy. For example list.sort will change the original list while sorted will return the new sorted list without changing the original one. So append is working on object so it will change the object value instead of returning new modified copy.
Hope this will make your view clear about append method.
What you are seeing is expected - the list.append method does not return the modified list. If you take a look at the relevant section of the Python tutorial, you'll notice that methods which return useful values are documented as such.
Working in the interactive interpreter, you can tell that the expression yields None by the lack of output:
>>> l = [1, 2, 3]
>>> l.append(14)
>>>
If (theoretically) list.append returned the list, you would see the following instead:
>>> l = [1, 2, 3]
>>> l.append(14)
[1, 2, 3, 14]
>>>
Using the interactive interpreter this way can save you from printing every value.
from python document
list.append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].
in simple word append function returns nothing but None
suggested reading