v Generator Expressions - Python

Generator Expressions

Interesting in learning more? Check out Fluent Python

# Create a list of students
students = ['Abe', 'Bob', 'Christina', 'Derek', 'Eleanor']
# Create a generator expression that yields lower-case versions of the student's names
lowercase_names = (student.lower() for student in students)
# View the generator object
lowercase_names
<generator object <genexpr> at 0x104837518>
# Get the next name lower-cased
next(lowercase_names)
'abe'
# Get the next name lower-cased
next(lowercase_names)
'bob'
# Get the remaining names lower-cased
list(lowercase_names)
['christina', 'derek', 'eleanor']