Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does append() always return None in Python? [duplicate]

list = [1, 2, 3] print(list.append(4))   ## WRONG, print does not work, append() returns None  ## RIGHT: list.append(4) print(list)  ## [1, 2, 3, 4] 

I'm learning Python and I'm not sure if this problem is specific to the language and how append is implemented in Python.

like image 999
starcodex Avatar asked May 20 '13 00:05

starcodex


People also ask

Does append return anything Python?

Python List append() method allows us to add any type of data to the end of the list. The method doesn't return anything.

Why does append overwrite Python?

1 Answer. This is really a matter of how Python handles assignments of lists. In both of your append commands, Python is pointing to the same list L. This is why the first list that you append appears to change.

Does Python append overwrite?

Bookmark this question. Show activity on this post. The first element of the list is overwritten.

Why it is necessary to have both the function append and extend?

Both these methods are used to manipulate the lists in their specific way. The append method adds a single or a group of items (sequence) as one element at the tail of a list. On the other hand, the extend method appends the input elements to the end as part of the original list.


2 Answers

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] print l + [4] # [1,2,3,4] print 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"]

like image 53
xuanji Avatar answered Oct 14 '22 22:10

xuanji


It is a convention in Python that methods that mutate sequences return None.

Consider:

>>> a_list = [3, 2, 1] >>> print a_list.sort() None >>> a_list [1, 2, 3]  >>> a_dict = {} >>> print a_dict.__setitem__('a', 1) None >>> a_dict {'a': 1}  >>> a_set = set() >>> print a_set.add(1) None >>> a_set set([1]) 

Starting in Python 3.3, this is now more explicitly documented:

Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.

The Design and History FAQ gives the reasoning behind this design decision (with respect to lists):

Why doesn’t list.sort() return the sorted list?

In situations where performance matters, making a copy of the list just to sort it would be wasteful. Therefore, list.sort() sorts the list in place. In order to remind you of that fact, it does not return the sorted list. This way, you won’t be fooled into accidentally overwriting a list when you need a sorted copy but also need to keep the unsorted version around.

In Python 2.4 a new built-in function – sorted() – has been added. This function creates a new list from a provided iterable, sorts it and returns it.

like image 28
Steven Rumbalski Avatar answered Oct 14 '22 22:10

Steven Rumbalski