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