[Tutor] Would somebody kindly...

Cameron Simpson cs at zip.com.au
Wed Oct 29 02:31:14 CET 2014


On 28Oct2014 18:02, Clayton Kirkwood <crk at godblessthe.us> wrote:
>!> Explain this double speak(>:
>!>
>Ah, Alan sent an answer also, but this one answers the last tidbit. Alan had the some_list and pair the same name, presumably creating a temporary tuple and when the loop is done, the temporary replaces the original.

Let me try a less wordy diagram. You will need to be displaying in a constant 
width font :-)

   [ pair for pair in values if key == pair[0] ]
     ^^^^-- the expression that accrues in the resulting list
              ^^^^-- the loop variable, taken from the loop source values
                      ^^^^^^-- the loop source values
                                ^^^^^^^^^^^^^^-- condition for including the
                                            expression in the resulting list

So that first "pair" could be any expression, it is almost only coincidence 
that it is the same as the loop variable. It is the same in this case because 
this is the idiomatic way to select particular values form an existing list.

If we'd wanted the new list to contain double the original values we'd write:

   [ pair*2 for pair in values if key == pair[0] ]

Cheers,
Cameron Simpson <cs at zip.com.au>

ERROR 155 - You can't do that.  - Data General S200 Fortran error code list


More information about the Tutor mailing list