On 29/10/14 01:02, Clayton Kirkwood wrote:
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.
Nope. If you re-read my post you'll seee that my examples are just expressions, they don;t assign the result. Dannys code is actually the equivalent of:
some_list = [pair for pair in values if key == pair[0]] some_list is an arbitrary name Danny used to represent the list that is built up by the comprehension. It doesn't actually have a name in the real comprehension. The name 'pair' in my code is used exactly as in Danny's 'for' loop below.
!####################### !some_list = [] !for pair in values: ! if key == pair[0]: ! some_list.append(pair) !#######################
There is no temporary tuples being created the list is built up inside the comprehension as the for part executes in the same way as in the expanded version. There is no magic here it's just a short-cut notation for the expanded loop (and it usually executes a bit faster). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor