[PYTHON] EP 16 Consider Generator Instead of Returning Lists

  • Using generators can be clearer than the alternative of returning lists of accumulated results

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.
  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))

Recommended Posts

EP 16 Consider Generator Instead of Returning Lists
EP 9 Consider Generator Expressions for Large Comprehensions
EP 4 Write Helper functions Instead of Complex Expressions
EP 23 Accept Functions for Simple Interfaces Instead of Classes
EP 7 Use List Comprehensions Instead of map and filter
Effective Python Note Item 16 Consider returning a generator without returning a list