Info
Content

Generator expressions

Generator expressions can be used to generate lists and dictionaries based on a condition in Python.

List generators
list = [x for x in things if x / 4 < 15] # add x to this list if x divided by four is more than 15
Dictionary generators
dict = {item: len(item) for item in words} # creates a dictionary of string lengths where words is a list of strings
No Comments
Back to top