Why Does Append() Always Return None in Python

Why does append() always return None in Python?

append is a mutating (destructive) operation (it modifies the list in place instead of of returning a new list). The idiomatic way to do the non-destructive equivalent of append would be

>>> l = [1,2,3]
>>> l + [4]
[1,2,3,4]
>>> l
[1,2,3]

to answer your question, my guess is that if append returned the newly modified list, users might think that it was non-destructive, ie they might write code like

m = l.append("a")
n = l.append("b")

and expect n to be [1,2,3,"b"]

Why does list.append() return None?

Just replace a_list = a_list.append(r) with a_list.append(r).

Most functions, methods that change the items of sequence/mapping does return None: list.sort, list.append, dict.clear ...

Not directly related, but see Why doesn’t list.sort() return the sorted list?.

Python Append to a list returns none

append does not return anything, so you cannot say

ListB = ListB.append('New_Field_Name')

It modifies the list in place, so you just need to say

ListB.append('New_Field_Name')

Or you could say

ListB += ['New_Field_Name']

Why does append('w') return None?

list.append() returns None to underscore the fact that it is a mutating call. Such calls, that modify their argument, generally return None. Calls that create a new list return it.

If it is important that list_test return the modified list, then your code might be better like this:

def list_test(foo):
foo.append('w')
return foo

my_input = [7,8,9]

>>> print list_test(my_input)
[7, 8, 9, 'w']
>>> print my_input
[7, 8, 9, 'w']

But, if I were writing this, I would follow the convention that mutating calls return None, and write it this way:

def list_test(foo):
foo.append('w')

my_input = [7,8,9]

>>> print list_test(my_input)
None
>>> print my_input
[7, 8, 9, 'w']

Finally, if you want list_test to be non-mutating, that is it should return a new list and not modify its input, one of these might work for you:

def list_test(foo):
new_foo = list(foo)
new_foo.append('w')
return new_foo

def list_test(foo):
return foo + ['w']

my_input = [7,8,9]

>>> print list_test(my_input)
[7, 8, 9, 'w']
>>> print my_input
[7, 8, 9]

Why That Code Returns 'None' (Tuples in List)

Instead of

rows = rows.sort(key=lambda x:x[3])

You want:

rows.sort(key=lambda x:x[3])

Because the .sort() will automatically update rows without the need to reassign like so:

posts = []
rows = [('02.02', 'title2', 'text2', 15, 1), ('01.02', 'title', 'text', 16, 1)]
rows.sort(key=lambda x:x[3]) #notice the change here
for i in range(2):
posts.append(rows[i])
print(posts)

Output:

[('02.02', 'title2', 'text2', 15, 1), ('01.02', 'title', 'text', 16, 1)]

The .sort() function updates rows, but returns None.

Why python returns None for variable that assign for itself?

append() method works in-place and returns None.

So, if you do the following, b will have the value None:

a = []
b = a.append("test")
print(b)

Why do we get a 'None' item when appending an item to an existing list?

list.append returns None. This is because list.append is an in-place operation. In addition, you are shadowing a built-in, which is not recommended.

You can append to a copy, then extend your original list:

L = ['a', 'b', 'c']
L_to_append = L.copy()
L_to_append.append('d')
L.extend(L_to_append)

But this is verbose. You can just use the += operator:

L = ['a', 'b', 'c']
L += L + ['d']

print(L)

['a', 'b', 'c', 'a', 'b', 'c', 'd']

How to append values to list?

it is mainly because of the fact that list.append method does not return anything, it appends the given value to the list in-place.

we can confirm this by the simple example below

a = list()
b = a.append(5)
>> print(b)
None
>> print(a)
[5]

As matter of fact,there is a simpler and more performant way to achieve this

>> a_list = [1,2,3]
>> a_list += list(range(4, 10))
>> print(a_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Here, we first shape a new list from a iterator range(4, 10),
then we concate the two list together to get the list we want by adding the new list to the original list a_list.

by doing this, we can avoid the overhead caused by repeatedly call the list.append method in a for loop



Related Topics



Leave a reply



Submit