Register to get access to free programming courses with interactive exercises

Generator expressions Python: Declarative programming

Dictionaries and sets help a lot when the information about elements is complete. It makes sense with incomplete sets or dictionaries because we cannot check for values or keys in other ways.

The situation is different with lists. A list is a sequence of items, so we need only traverse it once. And sometimes, you don't even need to go to the end of the list, for example, if we're looking for one specific item. It's important to remember that map and filter don't generate lists but generate new iterators based on iterator arguments instead.

Iterator-based data pipelines are efficient because they do nothing until the receiver party needs their result at the pipeline output. It is especially true when we combine them with functions from the itertools module.

List generators have another important feature — they create the whole list one way or another, even if they do not need all items from the list. Usually, you can interrupt the loop using break but cannot interrupt the list generator. Besides, it wouldn't look declarative. However, Python knows how to use iterator laziness in declarative code.

Generator expressions

We said above that sometimes sequences don't need to be computed. We'll also add that you seldom need to get and keep finished lists.

In those rare cases where you need a list, you can use list generators. But most problems are solved with generator expressions. They look like list generators. The only difference is they use round brackets instead of square brackets:

[x * x for x in range(10)]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
(x * x for x in range(10))
# <generator object <genexpr> at 0x7fe76f7e5db0>

As you can see, the result of calculating the second expression isn't a list but a generator object. We use it to postpone computing the elements of a sequence until we need them.

The generator objects are essentially iterators, so we cannot traverse them more than once:

def print6(xs):
    for i, x in enumerate(xs):
        print(x)
        if i == 5:
            break

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.