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


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

[Python 2.7.3] What's the difference between these two uses of "for"?

Started by"Yves S. Garret" <yoursurrogategod@gmail.com>
First post2013-03-17 17:58 -0700
Last post2013-03-18 00:37 -0400
Articles 16 — 8 participants

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


Contents

  [Python 2.7.3] What's the difference between these two uses of "for"? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-17 17:58 -0700
    Re: [Python 2.7.3] What's the difference between these two uses of "for"? Andrew Berg <bahamutzero8825@gmail.com> - 2013-03-17 20:24 -0500
    Re: [Python 2.7.3] What's the difference between these two uses of "for"? Gary Herron <gary.herron@islandtraining.com> - 2013-03-17 18:18 -0700
      Re: [Python 2.7.3] What's the difference between these two uses of "for"? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-17 18:59 -0700
      Re: [Python 2.7.3] What's the difference between these two uses of "for"? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-17 18:59 -0700
        Re: [Python 2.7.3] What's the difference between these two uses of "for"? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-17 19:14 -0700
          Re: [Python 2.7.3] What's the difference between these two uses of "for"? Dave Angel <davea@davea.name> - 2013-03-18 07:38 -0400
        Re: [Python 2.7.3] What's the difference between these two uses of "for"? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-17 19:14 -0700
          Re: [Python 2.7.3] What's the difference between these two uses of "for"? Roy Smith <roy@panix.com> - 2013-03-17 22:33 -0400
            Re: What's the difference between these two uses of "for"? alex23 <wuwei23@gmail.com> - 2013-03-18 18:08 -0700
          Re: [Python 2.7.3] What's the difference between these two uses of "for"? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 02:44 +0000
            Re: [Python 2.7.3] What's the difference between these two uses of "for"? Roy Smith <roy@panix.com> - 2013-03-17 22:51 -0400
    Re: [Python 2.7.3] What's the difference between these two uses of "for"? Roy Smith <roy@panix.com> - 2013-03-17 21:28 -0400
      Re: [Python 2.7.3] What's the difference between these two uses of "for"? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-17 19:01 -0700
        Re: [Python 2.7.3] What's the difference between these two uses of "for"? Roy Smith <roy@panix.com> - 2013-03-17 22:28 -0400
    Re: [Python 2.7.3] What's the difference between these two uses of "for"? Terry Reedy <tjreedy@udel.edu> - 2013-03-18 00:37 -0400

#41377 — [Python 2.7.3] What's the difference between these two uses of "for"?

From"Yves S. Garret" <yoursurrogategod@gmail.com>
Date2013-03-17 17:58 -0700
Subject[Python 2.7.3] What's the difference between these two uses of "for"?
Message-ID<485a3093-8c07-4d1a-b49e-af32f84f8198@googlegroups.com>
N00b question.  But here is the code:

http://bin.cakephp.org/view/709201806

In the first example, the first for-loop is run and then the list is assigned to the tricky variable.  But, what 
happens in the second example?  Does the loop after "in" get run only once or multiple number of times?

[toc] | [next] | [standalone]


#41378

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2013-03-17 20:24 -0500
Message-ID<mailman.3402.1363569881.2939.python-list@python.org>
In reply to#41377
On 2013.03.17 19:58, Yves S. Garret wrote:
> N00b question.  But here is the code:
> 
> http://bin.cakephp.org/view/709201806
> 
> In the first example, the first for-loop is run and then the list is assigned to the tricky variable.  But, what 
> happens in the second example?  Does the loop after "in" get run only once or multiple number of times?
> 
In the first example, sorted() returns a list, which is assigned to the
name tricky (Python doesn't have variables - names simply point to
objects in memory), and then the for loop iterates over tricky, which
points to a list. In the second example, the for loop iterates over the
list that sorted() returns. The only difference between the two is that
the list that sorted() returns is assigned to a name in the first example.

-- 
CPython 3.3.0 | Windows NT 6.2.9200 / FreeBSD 9.1

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


#41379

FromGary Herron <gary.herron@islandtraining.com>
Date2013-03-17 18:18 -0700
Message-ID<mailman.3403.1363570008.2939.python-list@python.org>
In reply to#41377
On 03/17/2013 05:58 PM, Yves S. Garret wrote:
> N00b question.  But here is the code:
>
> http://bin.cakephp.org/view/709201806
>
> In the first example, the first for-loop is run and then the list is assigned to the tricky variable.  But, what
> happens in the second example?  Does the loop after "in" get run only once or multiple number of times?

Just once.  The sorted fn is called just once, and the resulting list is 
iterated through.  In your first case, the list is bound to (assigned 
to) a name so it is accessible afterwards.  In the second case, it is 
available for garbage collection immediately after the loop finishes.

Gary Herron

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


#41381

From"Yves S. Garret" <yoursurrogategod@gmail.com>
Date2013-03-17 18:59 -0700
Message-ID<eb16ef13-f48f-42f8-bc38-f421be9982e0@googlegroups.com>
In reply to#41379
On Sunday, March 17, 2013 9:18:12 PM UTC-4, Gary Herron wrote:
> On 03/17/2013 05:58 PM, Yves S. Garret wrote:
> 
> > N00b question.  But here is the code:
> 
> >
> 
> > http://bin.cakephp.org/view/709201806
> 
> >
> 
> > In the first example, the first for-loop is run and then the list is assigned to the tricky variable.  But, what
> 
> > happens in the second example?  Does the loop after "in" get run only once or multiple number of times?
> 
> 
> 
> Just once.  The sorted fn is called just once, and the resulting list is 
> 
> iterated through.  In your first case, the list is bound to (assigned 
> 
> to) a name so it is accessible afterwards.  In the second case, it is 
> 
> available for garbage collection immediately after the loop finishes.
> 
> 
> 
> Gary Herron

Gotcha, thanks.

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


#41383

From"Yves S. Garret" <yoursurrogategod@gmail.com>
Date2013-03-17 18:59 -0700
Message-ID<mailman.3404.1363571995.2939.python-list@python.org>
In reply to#41379
On Sunday, March 17, 2013 9:18:12 PM UTC-4, Gary Herron wrote:
> On 03/17/2013 05:58 PM, Yves S. Garret wrote:
> 
> > N00b question.  But here is the code:
> 
> >
> 
> > http://bin.cakephp.org/view/709201806
> 
> >
> 
> > In the first example, the first for-loop is run and then the list is assigned to the tricky variable.  But, what
> 
> > happens in the second example?  Does the loop after "in" get run only once or multiple number of times?
> 
> 
> 
> Just once.  The sorted fn is called just once, and the resulting list is 
> 
> iterated through.  In your first case, the list is bound to (assigned 
> 
> to) a name so it is accessible afterwards.  In the second case, it is 
> 
> available for garbage collection immediately after the loop finishes.
> 
> 
> 
> Gary Herron

Gotcha, thanks.

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


#41384

From"Yves S. Garret" <yoursurrogategod@gmail.com>
Date2013-03-17 19:14 -0700
Message-ID<6ca68c8c-2567-4656-8e9b-dfa9409a997f@googlegroups.com>
In reply to#41383
I don't get why it's posting what I said twice...

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


#41410

FromDave Angel <davea@davea.name>
Date2013-03-18 07:38 -0400
Message-ID<mailman.3429.1363606721.2939.python-list@python.org>
In reply to#41384
On 03/17/2013 10:14 PM, Yves S. Garret wrote:
> I don't get why it's posting what I said twice...
>

Because you're using googlegroups, and haven't unchecked some poorly 
defined default setting.  You're posting both to python-list and to 
comp.lang.python, each of which is mirrored to the other.

That's one of at least two problems with posting via googlegroups.  The 
other is the stupid double-spacing of quotes, which is very annoying to 
others.


See:
   http://wiki.python.org/moin/GoogleGroupsPython


-- 
DaveA

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


#41385

From"Yves S. Garret" <yoursurrogategod@gmail.com>
Date2013-03-17 19:14 -0700
Message-ID<mailman.3405.1363573404.2939.python-list@python.org>
In reply to#41383
I don't get why it's posting what I said twice...

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


#41387

FromRoy Smith <roy@panix.com>
Date2013-03-17 22:33 -0400
Message-ID<roy-AAAF4C.22330017032013@70-1-84-166.pools.spcsdns.net>
In reply to#41385
In article <mailman.3405.1363573404.2939.python-list@python.org>,
 "Yves S. Garret" <yoursurrogategod@gmail.com> wrote:

> I don't get why it's posting what I said twice...

Because you're posting using the Google Groups web interface, right?  
Google Groups is just plain busted and double-posts everything (at least 
to this group).  Not your fault, but something to be aware of.  You'd do 
better to subscribe to the real email list, or use a traditional NNTP 
newsreader.

Google's motto may be "don't be evil", but they get to define what evil 
is.  Apparently working and playing well with mailing list technology 
which has worked just fine for literally decades isn't part of the 
definition.

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


#41468 — Re: What's the difference between these two uses of "for"?

Fromalex23 <wuwei23@gmail.com>
Date2013-03-18 18:08 -0700
SubjectRe: What's the difference between these two uses of "for"?
Message-ID<1b06c18e-9a1e-411a-96e2-b318fd6417f3@i5g2000pbj.googlegroups.com>
In reply to#41387
On Mar 18, 12:33 pm, Roy Smith <r...@panix.com> wrote:
> Google's motto may be "don't be evil", but they get to define what evil
> is.  Apparently working and playing well with mailing list technology
> which has worked just fine for literally decades isn't part of the
> definition.

Their decision to scrap Reader while ploughing forward with this
godawful new UI for Groups has pushed them into the "evil" basket for
me :(

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


#41388

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-18 02:44 +0000
Message-ID<51467f6f$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#41385
On Sun, 17 Mar 2013 19:14:49 -0700, Yves S. Garret wrote:

> I don't get why it's posting what I said twice...

Because you are emailing to the list, and CCing the list.

In your email, you have:

To: comp.lang.python@googlegroups.com
Cc: python-list@python.org


Unfortunately, they are the same thing. Or rather, when Google Groups 
receives its copy of the email, it then "helpfully" sends another copy to 
python-list@python.org even though you have already CCed it.

In defence of Google Groups, it's not *quite* as stupid as it appears, 
because it's not actually forwarding directly to the same email address. 
It is actually forwarding to the newsgroup comp.lang.python, which is an 
alias to python-list@python.org.

Confused? Don't be. It is very simple: there are at least three ways to 
post to this group:

1) Email to python-list@python.org

2) Post to the news group comp.lang.python on Usenet

3) Email to comp.lang.python@googlegroups.com

There are others as well. Think of them as all aliases to the same 
discussion forum. Whichever you choose, choose ONE ONLY. You are using 
two, hence there are two copies of your message.

I recommend that you choose 1) or 2) rather than Google Groups, if 
possible. There are two problems with Google Groups: 

- it is not very smart, and often mangles messages so that every line is 
separated by a blank line (although I see you have avoided that, at least 
so far!); 

- also, we get a lot of spam and junk advertising coming from Google 
Groups, and so in self-defence many people here have an automatic filter 
that junks anything from Google Groups unread.

If you are unable or unwilling to avoid Google Groups, we will still 
answer your questions, but keep in mind that many of the regulars here 
will not directly see your posts, but only replies to them.


-- 
Steven

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


#41389

FromRoy Smith <roy@panix.com>
Date2013-03-17 22:51 -0400
Message-ID<roy-907CC4.22512617032013@70-1-84-166.pools.spcsdns.net>
In reply to#41388
In article <51467f6f$0$6599$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

> there are at least three ways to 
> post to this group:
> 
> 1) Email to python-list@python.org
> 
> 2) Post to the news group comp.lang.python on Usenet
> 
> 3) Email to comp.lang.python@googlegroups.com

Amongst the ways, there are....

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


#41380

FromRoy Smith <roy@panix.com>
Date2013-03-17 21:28 -0400
Message-ID<roy-871BE1.21285617032013@70-1-84-166.pools.spcsdns.net>
In reply to#41377
In article <485a3093-8c07-4d1a-b49e-af32f84f8198@googlegroups.com>,
 "Yves S. Garret" <yoursurrogategod@gmail.com> wrote:

> N00b question.  But here is the code:
> 
> http://bin.cakephp.org/view/709201806
> 
> In the first example, the first for-loop is run and then the list is assigned 
> to the tricky variable.  But, what 
> happens in the second example?  Does the loop after "in" get run only once or 
> multiple number of times?

It's a little hard to answer your question because you're not using the 
right terminology.  When you say, 'the loop after "in"', I assume you 
mean:

[w for w in set(text2) if 'cie' in w or 'cei' in w]

yes?  That's not what most people would call "a loop".  It's a list 
comprehension.  For sure, there's an implied looping over the elements 
of set(text2) in there, but that's not the way people refer to it.

Anyway, here's what happens.  Working from the inside out...

First, set(text2) is evaluated.  I assume text2 is something like a list 
of strings, or at least iterable which yields strings.  This results in 
a set object being created.  Let's call that set S.

Next, the list comprehension gets evaluated:

[w for w in s if 'cie' in w or 'cei' in w]

This iterates over the the elements of s and forms a list out of those 
elements which pass the condition in the 'if' clause.  This results in a 
list object being created. Let's call that L1

Next, sorted(L1) is evaluated.  This returns another list (call it L2).

Finally, we get to:

for word in L2:
   print word,

This iterates over all the elements in L2, assigning each one, in turn 
to word, and executing the body of the for statement.

Does that answer your question?  I'm sure other people will point out 
that this is not the most efficient way to do this (your way is not 
wrong, it's just not the most efficient way).  There's a way to write 
this which avoids creating L1.  That could be important if there's a 
large amount of data involved.

But, make sure you fully understand what's happing in the example you 
gave before you move on to the next step.

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


#41382

From"Yves S. Garret" <yoursurrogategod@gmail.com>
Date2013-03-17 19:01 -0700
Message-ID<7f3b4dde-fbd7-44ca-96bc-31a6b2894200@googlegroups.com>
In reply to#41380
On Sunday, March 17, 2013 9:28:56 PM UTC-4, Roy Smith wrote:
> In article <485a3093...@googlegroups.com>,
> 
>  "Yves S. Garret" <your...@gmail.com> wrote:
> 
> 
> 
> > N00b question.  But here is the code:
> 
> > 
> 
> > http://bin.cakephp.org/view/709201806
> 
> > 
> 
> > In the first example, the first for-loop is run and then the list is assigned 
> 
> > to the tricky variable.  But, what 
> 
> > happens in the second example?  Does the loop after "in" get run only once or 
> 
> > multiple number of times?
> 
> 
> 
> It's a little hard to answer your question because you're not using the 
> 
> right terminology.  When you say, 'the loop after "in"', I assume you 
> 
> mean:
> 
> 
> 
> [w for w in set(text2) if 'cie' in w or 'cei' in w]
> 
> 
> 
> yes?  That's not what most people would call "a loop".  It's a list 
> 
> comprehension.  For sure, there's an implied looping over the elements 
> 
> of set(text2) in there, but that's not the way people refer to it.
> 
> 
> 
> Anyway, here's what happens.  Working from the inside out...
> 
> 
> 
> First, set(text2) is evaluated.  I assume text2 is something like a list 
> 
> of strings, or at least iterable which yields strings.  This results in 
> 
> a set object being created.  Let's call that set S.
> 
> 
> 
> Next, the list comprehension gets evaluated:
> 
> 
> 
> [w for w in s if 'cie' in w or 'cei' in w]
> 
> 
> 
> This iterates over the the elements of s and forms a list out of those 
> 
> elements which pass the condition in the 'if' clause.  This results in a 
> 
> list object being created. Let's call that L1
> 
> 
> 
> Next, sorted(L1) is evaluated.  This returns another list (call it L2).
> 
> 
> 
> Finally, we get to:
> 
> 
> 
> for word in L2:
> 
>    print word,
> 
> 
> 
> This iterates over all the elements in L2, assigning each one, in turn 
> 
> to word, and executing the body of the for statement.
> 
> 
> 
> Does that answer your question?  I'm sure other people will point out 
> 
> that this is not the most efficient way to do this (your way is not 
> 
> wrong, it's just not the most efficient way).  There's a way to write 
> 
> this which avoids creating L1.  That could be important if there's a 
> 
> large amount of data involved.
> 
> 
> 
> But, make sure you fully understand what's happing in the example you 
> 
> gave before you move on to the next step.

Hi, thanks for the detailed explanation.  And yes, you've answered my question.

I'm trying to better understand what's going on behind the scenes and I
appreciate your thorough input.  What I don't understand is, how would you
avoid creating L1?

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


#41386

FromRoy Smith <roy@panix.com>
Date2013-03-17 22:28 -0400
Message-ID<roy-AB8126.22283217032013@70-1-84-166.pools.spcsdns.net>
In reply to#41382
In article <7f3b4dde-fbd7-44ca-96bc-31a6b2894200@googlegroups.com>,
 "Yves S. Garret" <yoursurrogategod@gmail.com> wrote:

> I'm trying to better understand what's going on behind the scenes and I
> appreciate your thorough input.  What I don't understand is, how would you
> avoid creating L1?

Leave out the square brackets in:

sorted([w for w in set(text2) if 'cie' in w or 'cei' in w])

If you re-write that as:

sorted(w for w in set(text2) if 'cie' in w or 'cei' in w)

Now you've got what's called a generator expression.  This iterates over 
the same values as the list comprehension would, but it generates them 
one at a time, so it doesn't have to store them all somewhere.  It 
essentially a really neat syntax for writing coroutines.

As usual, Stack Overflow does a pretty good job explaining this:

http://stackoverflow.com/questions/47789/

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


#41395

FromTerry Reedy <tjreedy@udel.edu>
Date2013-03-18 00:37 -0400
Message-ID<mailman.3410.1363581489.2939.python-list@python.org>
In reply to#41377
On 3/17/2013 8:58 PM, Yves S. Garret wrote:
> N00b question.  But here is the code:
>
> http://bin.cakephp.org/view/709201806

Short code like this should be included in your message.

tricky = sorted([w for w in set(text2) if 'cie' in w or 'cei' in w])
for word in tricky:
   print word,

for word in sorted([w for w in set(text2) if 'cie' in w or 'cei' in w]):
   print word,

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


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


csiph-web