Disappearing lists

The list methods append() and sort() change a list in place but don't return the list. They return None. Don't use these methods in assignment statements or else the list might disappear, as in this example:

>>> L = ["my", "list"]


To append to a list, just use the append() method by itself. Don't give the result a name.

>>> L = ["my", "list"] >>> L.append('x') >>> print L ['my', 'list', 'x']

Was this article helpful?

0 0

Post a comment