Why Don't These List Operations Return the Resulting List

Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?

The general design principle in Python is for functions that mutate an object in-place to return None. I'm not sure it would have been the design choice I'd have chosen, but it's basically to emphasise that a new object is not returned.

Guido van Rossum (our Python BDFL) states the design choice on the Python-Dev mailing list:

I'd like to explain once more why I'm so adamant that sort() shouldn't
return 'self'.

This comes from a coding style (popular in various other languages, I
believe especially Lisp revels in it) where a series of side effects
on a single object can be chained like this:

x.compress().chop(y).sort(z)

which would be the same as

x.compress()
x.chop(y)
x.sort(z)

I find the chaining form a threat to readability; it requires that the
reader must be intimately familiar with each of the methods. The
second form makes it clear that each of these calls acts on the same
object, and so even if you don't know the class and its methods very
well, you can understand that the second and third call are applied to
x (and that all calls are made for their side-effects), and not to
something else.

I'd like to reserve chaining for operations that return new values,
like string processing operations:

y = x.rstrip("\n").split(":").lower()

There are a few standard library modules that encourage chaining of
side-effect calls (pstat comes to mind). There shouldn't be any new
ones; pstat slipped through my filter when it was weak.

Why does append() always return None in Python?

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

Why does return list.sort() return None, not the list?

list.sort sorts the list in place, i.e. it doesn't return a new list. Just write

newList.sort()
return newList

Issue in Python lists

append() method does not return any value but updates existing list.
if you want to see the updated list, use print(list_b).
print(t) or print(t1) will return None as they don't have any return values.

Unable to append item in a list after slicing it python

This is because z does not contain the list but the y does.

List is mutable in python, which means,

y = list[:2]
z = y.append(num_4)

the above code will add num_4 to y and append() function returns none hence z will contain none.

So print y not z

Why can't I sort a list right after I make it?

.sort() sorts the list in place and returns None. You need to use the sorted() function here.

>>> a = [3, 2, 1]
>>> print a.sort()
None
>>> a
[1, 2, 3]
>>> sorted(a)
[1, 2, 3]


Related Topics



Leave a reply



Submit