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


Groups > comp.lang.python > #99563

Re: What does a list comprehension do

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
Newsgroups comp.lang.python
Subject Re: What does a list comprehension do
Date Thu, 26 Nov 2015 12:52:45 +0100
Lines 34
Message-ID <mailman.132.1448538769.20593.python-list@python.org> (permalink)
References <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <877fldnm9z.fsf@handshake.de> <mailman.73.1448459485.20593.python-list@python.org> <pan.2015.11.26.11.13.43.440000@nowhere.invalid>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de MZWca7vDRm8ruFNOlWgJegkGUNFGn/vXzLKvWr+it23A==
Return-Path <antoon.pardon@rece.vub.ac.be>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:134': 0.05; 'subject:skip:c 10': 0.07; '(0,': 0.09; 'expression:': 0.09; 'python': 0.10; 'python.': 0.11; 'translate': 0.15; '(3,': 0.16; 'lambda': 0.16; 'received:ac.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'version.': 0.18; '>>>': 0.20; 'exceptions': 0.22; 'seems': 0.23; 'second': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'behaviour': 0.29; 'received:be': 0.30; 'surprised': 0.33; 'list': 0.34; 'gets': 0.35; 'could': 0.35; 'returning': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'seem': 0.37; 'itself': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'worth': 0.67; 'act': 0.67; 'schreef': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Am8JAMnxVlaGuA9G/2dsb2JhbABehFcBwBuGDwKCCgEBAQEBAYVAAQEDASNmCxoCBSECAg8CRhMIAogiCK4HjBuEHAEBCAIhgQGFU4R+hSeCToFEBZZXjTWJEJNOY4QFhlEBAQE
User-Agent Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.8.0
In-Reply-To <pan.2015.11.26.11.13.43.440000@nowhere.invalid>
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>
Xref csiph.com comp.lang.python:99563

Show key headers only | View raw


Op 26-11-15 om 12:13 schreef Nobody:
> Returning to the original expression:
>
> 	> q = [lambda x: i * x for i in range(4)]
> 	> q[0](1), q[3](1)
> 	(3, 3)
> 	> q = [lambda x,i=i: i * x for i in range(4)]
> 	> q[0](1), q[3](1)
> 	(0, 3)

Personnaly I would prefer:

>>> q = [(lambda i: lambda x: i * x)(i) for i in range(4)]
>>> q[0](1), q[3](1)
(0, 3)

And this is where I ask whether it would be worth the effort to change
the behaviour of python.

In general the following two seem equivallent:

   [<expression> for x in <iter>] and [(lambda x: <expression>)(x) for x in <iter>]

The only exceptions seems to be when <expression> is itself a lambda.

It also seems that people who try this for the first time are surprised
with what they get and seem to expect there list comprehension to
act as if they had written the second version.

So would it be advisable if python would translate the expression people
write into a lambda that gets called?

Are there issues that could come up?

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


Thread

What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?)) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-25 14:51 +0100
  Re: What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?)) Nobody <nobody@nowhere.invalid> - 2015-11-26 11:13 +0000
    Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 12:52 +0100
      Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 14:56 +0200
        Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 14:33 +0100
          Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 15:56 +0200
            Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 15:26 +0100
              Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 17:36 +0200
                Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 20:18 +0100
              Re: What does a list comprehension do Jussi Piitulainen <harvest@is.invalid> - 2015-11-26 18:11 +0200

csiph-web