Thursday, June 27, 2013

Changing Python list comprehensions to generator comprehensions

The list comprehension ...

    assert all([x % 2 == 0 for x in sequence])

... can be rewritten as the generator comprehension ...

    assert all(x % 2 == 0 for x in sequence)

... instead.

You can use the Vim substitution :%s/all(\[\(.*\)\])/all(\1)/g to change all([...]) to all(...) everywhere in a file.

No comments: