There is a scenario in which I got a little confused.
item = [5]
item.append(item.append(6))
print(item)
## This will result
[5, 6, None]
Why there is None as the last element of the list?
There is a scenario in which I got a little confused.
item = [5]
item.append(item.append(6))
print(item)
## This will result
[5, 6, None]
Why there is None as the last element of the list?
Because item.append(6)
returns None
, but adding 6
to the list during execution, so result expression converting to this:
item.append(None)
This behavior described here:
Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.
item.append(6) appends 6 to the list and returns None.
Since you do item.append(item.append(6)), the inner append() appends 6 and return None, the outer append() appends the 'None' returned by the inner append() fn. just remove the outer append() function to solve your problem. Since the append() function returns only None, you cannot print the list like that. Follow the code below.
Like this:
item = [5]
item.append(6)
print(item)
I guess you missed the [] while writing the question.
Update after you updated your question: item.append(6) returns None, so when you append item.append(6) to your list again, then it'll append None to it.