List Comprehensions
You can split a list comprehension over multiple lines to make them easier to read.
[ x.some_function()
for x in some_list
if math_element(x) ]
You can pass list comprehensions directly into functions that accept lists without the need for square brackets []
to indicate the lists creation.
some_list_function(
some_function(x)
for x in some_list)
There is a filter
function built into python that takes a list and a predicate to filter that list again. However, its much easier to use the filter built into list comprehension syntax with the if
statement, if you want to operate on the contents of the list at the same time rather than just filter it.