[Tutor] selecting specific items from a list

alan.gauld@bt.com alan.gauld@bt.com
Tue, 8 May 2001 17:23:06 +0100


> Let's say I have the following list:
> 
> [(3,3),(2,0),(5,5), (1,1), (4,5), (a,a), (a,e), (b,b), (c,d)]
> 
> How can I tell the computer to return to me only those items 
> in the list for 
> which x==y.  That is, [(3,3), (5,5), (1,1), (a,a), (b,b)].

Thats exactly what reduce() is for.

def sames(t): return t[0] == t[1]  # test an element
newlist = reduce(sames, juliettelist) #use the test as an argument

Take a look at my Functional Programming for an intro to these
types of functions:

http://www.crosswinds.net/~agauld/tutfctnl.htm

Alan G