Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93156 > unrolled thread

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

Started byfl <rxjwg98@gmail.com>
First post2015-06-25 11:14 -0700
Last post2015-06-25 22:01 +0300
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Could you explain "[1, 2, 3].remove(2)" to me? fl <rxjwg98@gmail.com> - 2015-06-25 11:14 -0700
    Re: Could you explain "[1, 2, 3].remove(2)" to me? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-25 12:37 -0600
    Re: Could you explain "[1, 2, 3].remove(2)" to me? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-25 22:01 +0300

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

Fromfl <rxjwg98@gmail.com>
Date2015-06-25 11:14 -0700
SubjectCould you explain "[1, 2, 3].remove(2)" to me?
Message-ID<e5890b67-939e-432a-b1ef-521bef393ae2@googlegroups.com>
Hi,

I see a code snippet online:

[1, 2, 3].remove(42)

after I modify it to:

[1, 2, 3].remove(2)

and

aa=[1, 2, 3].remove(2)


I don't know where the result goes. Could you help me on the question?

Thanks,

[toc] | [next] | [standalone]


#93157

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-06-25 12:37 -0600
Message-ID<mailman.75.1435257492.3674.python-list@python.org>
In reply to#93156
On Thu, Jun 25, 2015 at 12:14 PM, fl <rxjwg98@gmail.com> wrote:
> Hi,
>
> I see a code snippet online:
>
> [1, 2, 3].remove(42)

I don't know where you pulled this from, but if this is from a
tutorial then it doesn't seem to be a very good one.

This constructs a list containing the elements 1, 2, and 3, and
attempts to remove the non-existent element 42, which will raise a
ValueError.

> after I modify it to:
>
> [1, 2, 3].remove(2)

This at least removes an element that is actually in the list, so it
won't throw an error, but the list is then discarded, so nothing was
actually accomplished by it.

> and
>
> aa=[1, 2, 3].remove(2)

This does the same thing, but it sets the variable aa to the result of
the *remove* operation. The remove operation, as it happens, returns
None, so the the list is still discarded, and the only thing
accomplished is that aa is now bound to None.

> I don't know where the result goes. Could you help me on the question?

You need to store the list somewhere before you start calling
operations on it. Try this:

aa = [1, 2, 3]
aa.remove(2)

Now you have the list in the variable aa, and the value 2 has been
removed from it.

[toc] | [prev] | [next] | [standalone]


#93160

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2015-06-25 22:01 +0300
Message-ID<lf5lhf7vchx.fsf@ling.helsinki.fi>
In reply to#93156
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web