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


Groups > comp.lang.python > #93157

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

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; 'none,': 0.05; 'removes': 0.05; '[1,': 0.09; 'none.': 0.09; 'question?': 0.09; 'snippet': 0.09; 'throw': 0.09; 'thu,': 0.15; '42,': 0.16; 'constructs': 0.16; 'operation,': 0.16; 'operation.': 0.16; 'subject:Could': 0.16; 'wrote:': 0.16; 'element': 0.18; 'variable': 0.20; '2015': 0.23; 'elements': 0.23; 'sets': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'raise': 0.24; 'somewhere': 0.24; 'least': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'accomplished': 0.29; 'online:': 0.29; 'tutorial': 0.29; 'operations': 0.31; 'code': 0.31; 'subject:?': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'to:addr:python- list': 0.35; 'list': 0.35; 'but': 0.36; 'list,': 0.36; 'hi,': 0.37; 'subject:: ': 0.37; "won't": 0.38; 'skip:3 10': 0.38; 'pm,': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'seem': 0.39; 'where': 0.40; 'to:name:python': 0.84; 'subject:you': 0.88; 'thing,': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=GNgXJioyxFN3eMcgnd6ewiAvMfSoaeJQjkPEguxb+P4=; b=dboLIPTdhvsXqb9VUSsFIUAIOn8CNBDS/6ZAShGKTUD7bKvWsHq+pIVfbSGxwU5ryF P6CnFHLqKqi5c170gzuvA+TYfrRfNOueMfUTAVWqLWQEuHwgyOI+Xk2OtGwwzOB7uR5s tQG3+UwdwmzAvBSGY/0GwpNfQprAtiuuSqHF2pXkVEOtdRZ6yVK9geHuxxkocxc3CGIx +pjQFZmDMQEX2Uolsj2U5x+pSvYF1X32XgYTDHonY2tuZg+Rai/6pkCYwtTcz7BSGUEH UEjEnLfWvHeqUlO+mioReyGO1X/StkaHLTQ0hlq76N2x1nfy6yEkJekfuqDa7FAfefiF c+Gg==
X-Received by 10.170.110.82 with SMTP id c79mr57606345ykb.113.1435257484044; Thu, 25 Jun 2015 11:38:04 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <e5890b67-939e-432a-b1ef-521bef393ae2@googlegroups.com>
References <e5890b67-939e-432a-b1ef-521bef393ae2@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 25 Jun 2015 12:37:24 -0600
Subject Re: Could you explain "[1, 2, 3].remove(2)" to me?
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.75.1435257492.3674.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1435257492 news.xs4all.nl 2881 [2001:888:2000:d::a6]:33921
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:93157

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web