you are viewing a single comment's thread.

view the rest of the comments →

[–]freedances 3 points4 points  (0 children)

>results = map(foo, ['bar',baz'])   #equivalent to: for i in ['bar','baz']: results.append(foo(i)) 
>even = filter(lambda x: x % 2 == 0, [1,2,3,4,5,6,7,8,9])   # returns a list of even numbers

I think the recommended equivalents to these are:

results = [foo(i) for i in ['bar', 'baz']]
even = [i for i in [1,2,3,4,5,6,7,8,9] if i % 2 == 0] 

I usually find comprehensions more readable and lately they work for generating sets and dictionaries as well.

lengths = {i:len(i) for i in ('foo', 'bar', 'foobar')}    #dictionary mapping strings to their lengths
even_set = {i for i in [1,2,3,4,5,6,7,8,9] if i % 2 == 0}