Reputation: 4264
Why does the following code make a equal to "None":
a = [1]
a = a.append(0) if len(a) == 1 else a
This happens in both Python 2 and 3.
Upvotes: 0
Views: 40
Reputation: 40894
It just so happens that list.append
does not return the list. It mutates it and returns nothing (None
).
In your case, a = a +[0] if len(a) == 1 else a
would work. But the following is simpler: if len(a) == 1: a.append(0)
.
Upvotes: 1
Reputation: 13790
append()
does not return a value. Just do this:
a = [1]
if len(a) == 1:
a.append(0)
Upvotes: 3