[Tutor] Would somebody kindly...

Clayton Kirkwood crk at godblessthe.us
Wed Oct 29 05:33:10 CET 2014


!-----Original Message-----
!From: Cameron Simpson [mailto:cs at zip.com.au]
!Sent: Tuesday, October 28, 2014 6:31 PM
!To: Clayton Kirkwood
!Cc: 'Danny Yoo'; 'Python Tutor Mailing List'
!Subject: Re: [Tutor] Would somebody kindly...
!
!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] ]

Ok, I am somewhat confused again. In the original example up above, it
appears that the pair list or tuple gets overridden. In this one right
above, once again, the list gets overwritten again, but what is being
doubled?, each part of the tuple?

Clayton

!
!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