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


Groups > comp.lang.python > #99535

Re: What does a list comprehension do

From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: What does a list comprehension do
Date 2015-11-26 08:17 +0100
Message-ID <mailman.116.1448522274.20593.python-list@python.org> (permalink)
References <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <877fldnm9z.fsf@handshake.de> <5655BCDB.4020905@rece.vub.ac.be>

Show all headers | View raw


Antoon Pardon <antoon.pardon@rece.vub.ac.be> writes:

> Op 20-11-15 om 08:49 schreef dieter:
>> In addition, the last few days have had two discussions in this list
>> demonstrating the conceptial difficulties of late binding -- one of them:
>>
>>       Why does "[lambda x: i * x for i in range(4)]" gives
>>       a list of essentially the same functions?
>
> Can you (or someone else) explain what a list comprehension is equivallent of.
> Especially in python3.

I am not sure about "Python3" (never used it), but in Python 2,
the simple list comprehension "[x for x in l]" is roughly equivalent to:

    result = []
    for x in l: result.append(x)
    ... the list comprehension result is in "result" which (however) is not bound ...

In Python 3, "x" might not be bound as well - to harmonise list comprehension
with generator expressions (and avoid the confusion, that a local
(temporary) binding can change the value of a global binding).

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


Thread

Re: What does a list comprehension do dieter <dieter@handshake.de> - 2015-11-26 08:17 +0100

csiph-web