[Tutor] A couple beginner questions.

Ignacio Vazquez-Abrams ignacio@openservices.net
Tue, 25 Sep 2001 02:08:02 -0400 (EDT)


On Mon, 24 Sep 2001, Sean 'Shaleh' Perry wrote:

> [1,2,3] is a list in python.  There are two handy functions in python for
> lists:
>
> map(func,list) -- every item in list is passed to func() as an argument this is
> equivalent to 'for i in list: func(i)'
>
> reduce(func, list) -- starting with the first two elements, pass two elements
> to func and keep a cumalitive result.  This is the same as 'total =
> func(list[0], list[1]); for i in list[2:]: total = func(total, i)'

Actually, there are three:

filter(func, list) -- returns in a list each element of the list the
func(element) returns true for:

filter(lambda x: x!=3, [1, 2, 3, 4, 5]) returns [1, 2, 4, 5]

Also, map() returns a list containing the results of the function applied to
the elements.

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>