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.
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.
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.
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: .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With