Siph0n
October 17th, 2008, 02:55 AM
I am trying to learn about map and filter in python, and I don't see a difference between them and for loops
def f(x): return x % 2 != 0 and x % 3 != 0
filter(f, range(2, 25))
vs
a = []
for i in range(2,25):
if i % 2 != 0 and i % 3 !=0:
a.append(i)
and
def cube(x): return x*x*x
map(cube, range(1, 11))
vs
a = []
for i in range(1, 11):
a.append(i*i*i)
I see that using filter and map are a couple of lines smaller, but which ones are better to use, and why???
Thanks!!
def f(x): return x % 2 != 0 and x % 3 != 0
filter(f, range(2, 25))
vs
a = []
for i in range(2,25):
if i % 2 != 0 and i % 3 !=0:
a.append(i)
and
def cube(x): return x*x*x
map(cube, range(1, 11))
vs
a = []
for i in range(1, 11):
a.append(i*i*i)
I see that using filter and map are a couple of lines smaller, but which ones are better to use, and why???
Thanks!!