Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does list.append evaluate to false in a boolean context? [duplicate]

Tags:

python

list

Is there a reason being list.append evaluating to false? Or is it just the C convention of returning 0 when successful that comes into play?

>>> u = [] >>> not u.append(6) True 
like image 809
diciu Avatar asked Nov 05 '09 18:11

diciu


People also ask

Does list append make a copy?

💡 Tips: When you use . append() the original list is modified. The method does not create a copy of the list – it mutates the original list in memory.

Why do we use append () method of list in push operation?

append() will place new items in the available space. Lists are sequences that can hold different data types and Python objects, so you can use . append() to add any object to a given list. In this example, you first add an integer number, then a string, and finally a floating-point number.

Does append modify the list?

Every element of the list turns to the newly appended element. For example, after 4 squares of the maze, the 4 elements in past are changed to whatever the appended element is.

Why append function is used?

Append in Python is essential to add a single item at the end of a list, array, deque, or other collection types and data structures on the go.


1 Answers

Most Python methods that mutate a container in-place return None -- an application of the principle of Command-query separation. (Python's always reasonably pragmatic about things, so a few mutators do return a usable value when getting it otherwise would be expensive or a mess -- the pop method is a good example of this pragmatism -- but those are definitely the exception, not the rule, and there's no reason to make append an exception).

like image 172
Alex Martelli Avatar answered Sep 18 '22 18:09

Alex Martelli