Python list comprehension
List Comprehension
A list comprehension looks like this:
new_list = [f(o) for o in a_list if o>0]
This will return every element of a_list
that is greater than 0, after passing it to the function f
. There are three parts here: the collection you are iterating over (a_list
), an optional filter (if o>0
), and something to do to each element (f(o)
).