Help With filter().
Remco Gerlich
scarblac at pino.selwerd.nl
Thu Jul 12 05:01:27 EDT 2001
EricIDLE <grayson-wilson at home.com> wrote in comp.lang.python:
> I Still Dont Get It. Could you explain it in simpiler terms?
Filter 'filters' a list, it lets through only those elements of a list for
which some condition is true.
For instance, say you have a list of numbers 0 to 20 (range(21)), and you
want to have only those that are divisable by three.
Then you make a small function that is true when a number is divisable by
three:
def divisable_by_three(x):
if x % 3 == 0:
return 1
else:
return 0
Now you can use that to filter the list:
filter(divisable_by_three, range(21))
That should return [0, 3, 6, 9, 12, 15, 18].
--
Remco Gerlich
More information about the Python-list
mailing list