LoginSignup
1
1

More than 5 years have passed since last update.

EP 16 Consider Generator Instead of Returning Lists

Posted at
  • Using generators can be clearer than the alternative of returning lists of accumulated results
  • The iterator returned by a generator produces the set of values passed to yield expressions within the generator function's body
  • Generators can produce a sequence of outputs for arbitrarily large inputs becuase their working memory doesn't include all inputs and outputs.

Effective Python

The simplest choice for functions that produce a sequence of results is to return a list of items.

def index_words(text):
    result = []
    if text:
        result.append(0)
    for index, letter in enumerate(text):
        if letter == ' ':
            result.append(index + 1)
    return result

address = 'Four score and seven years ago...'
result = index_words(address)
print(result[:3])

[0, 5, 11]

There are two problems with the index_word function.

  1. the code is a bit dense and noisy.
    • Each time a new result is found, I call the append method.
  • There is one line ofr creating the result list and another for returning it.
  1. It requires all results to be stored in the list before bneing returned. This is effective in the case where we read huge file
def index_words_iter(text):
    if text:
        yield 0
    for index, letter in enumerate(text):
        if letter == ' ':
            yield index + 1


def index_file(handle):
    offset = 0
    for line in handle:
        if line:
            yield offset
        for letter in line:
            offset += 1
            if letter == '':
                yield offset

from itertools import islice

with open('/tmpaddress.txt') as f:
    it = index_file(f)
    result = islice(it, 0, 3)
    print(list(result))

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1