Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Why does extend() and append() return None (void)? [duplicate]

Possible Duplicate:
why does python's list.append evaluate to false?

In my opinion, I think list1.extend(list2) and list1.append(num) should return the mutated list rather than None (void). As well as having the side effect of destructively mutating the original list.

like image 303
user1352399 Avatar asked May 21 '12 00:05

user1352399


People also ask

Why is my append function returning None Python?

append() is an in-place operation, meaning that it modifies the state of the list , instead of returning a new list object. All functions in Python return None unless they explicitly return something else. The method a. append() modifies a itself, which means that there's nothing to return.

Does extend () return None?

Python List extend() Returns None. The return value of the extend() method is None . The return value of the extend() method is not a list with the added elements.

What is the difference between append () and extend () methods in Python?

append() adds a single element to the end of the list while . extend() can add multiple individual elements to the end of the list. Argument: .

What does extend return in Python?

The extend() method extends the list by appending all the items from the iterable to the end of the list. This method does not return anything; it modifies the list in place.


1 Answers

I believe the intent was to promote readable code and reduce bugs. This decision was made a very long time ago, but you can probably find more by looking at the archives of the python/python-dev mailing lists.

Python3 would have been the opportunity to change this behaviour, but as you see it remains, so is not considered a design mistake by the development team

like image 179
John La Rooy Avatar answered Sep 22 '22 18:09

John La Rooy