[Tutor] (no subject)

lonetwin@yahoo.com lonetwin@yahoo.com
Fri, 25 Jan 2002 02:08:35 -0500


UIT
Received: from mercury.worli (localhost.localdomain [127.0.0.1])
	by mercury.worli (Postfix) with SMTP
	id 3B9D69137; Fri, 25 Jan 2002 12:29:43 +0530 (IST)
Content-Type: text/plain;
  charset="iso-8859-1"
From: lonetwin <lonetwin@yahoo.com>
Reply-To: lonetwin@yahoo.com
To: kp87@lycos.com, tutor@python.org
Subject: Re: [Tutor] Re: List Comprehensions again
Date: Fri, 25 Jan 2002 12:29:43 +0530
X-Mailer: KMail [version 1.2]
References: <PBNCAJJDNHACLAAA@mailcity.com>
In-Reply-To: <PBNCAJJDNHACLAAA@mailcity.com>
MIME-Version: 1.0
Message-Id: <02012512294300.04555@mercury.worli>
Content-Transfer-Encoding: 8bit

Hey All,
   
On Thursday 24 January 2002 22:24, kevin parks wrote:
> I have never been able to get my head around list comprehension and
> desperately want too. I wish that there was a *good* little 2-3 page toot
> on list comprehensions that start from the very clearest and simplest
> examples and built up from there with nice explanations in between.

   Ok, somehow I find it wierd how some of people find list comprehension 
un-pythonic ....like I said in my post, couple of days back, they make 
complete sense to me. Lemme see if I can probably get you to see it the way 
that I do :
 when I say:

   P = func(i)

I'll get the
 return value of calling func(),
 called with the argument i,
 which is then assigned to P,

   Now suppose I have a list (or for that matter any sequence) called 
SomeSequence and I want to call the func() function for every element of 
SomeSequence, I do:

   for x in SomeSequence:
       func(x)

   Now, if I want to save all the return values in a list, I do:

   l = []
   for x in SomeSeruence:
       l.append(func(x))

   Still with me ??

   The python way to express it is:
o I want a list ..................... l = []

o which contains the return values... l = [ func(x) ]
  got by calling function func()

o for every element in SomeSequence.. l = [ func(x) for x in SomeSequence ]

like I said in my post earlier:

 [ func(x) for x in SomeList ]
 Return a list filled with func(x)'s for every x in SomeList

  Looks/Sounds almost the same in english as well as python, so does

 [ x.func() for x in SomeList if x == Something ]

 Return a list filled with func(x)'s for every x in SomeList that equals 
Something.

I really hope that helps :)

Peace
Steve

-- 
"'Tis true, 'tis pity, and pity 'tis 'tis true."
-- Poloniouius, in Willie the Shake's _Hamlet, Prince of Darkness_