Could you explain "[1, 2, 3].remove(2)" to me?

Jussi Piitulainen jpiitula at ling.helsinki.fi
Thu Jun 25 15:01:14 EDT 2015


fl writes:

> aa=[1, 2, 3].remove(2)
>
> I don't know where the result goes. Could you help me on the question?

That method modifies the list and returns None (or raises an exception).

Get a hold on the list first:

aa=[1, 2, 3]

*Then* call the method. Just call the method, do not try to store the
value (which will be None) anywhere:

aa.remove(2)

*Now* you can see that the list has changed. Try it.

By the way, it's no use to try [1, 2, 3].remove(2). That will only
modify and throw away a different list that just happens to have the
same contents initially. Try these two:

aa=[1, 2, 3]
bb=[1, 2, 3]  # a different list!

aa=[1, 2, 3]
bb=aa         # the same list!

In both cases, try removing 2 from aa and then watch what happens to aa
and what happens to bb.



More information about the Python-list mailing list