Ambrosio
Ambrosio

Reputation: 4099

What's the most Pythonic way to refactor this list building code?

I have a list of results that I need to pull out various other lists from. For example, all owners that are male, all cars between 5 and 10 years old.

def get_male_owners(self):
    results = []
    for result in self.results:
        if result.owner.sex.male:
            results.append(result)
    return results

def get_cars_5_to_10(self):
    results = []
    for result in self.results:
        if result.car:
            if self.is_5_to_10(result.car):
                results.append(result)
    return results

def is_5_to_10(self, car):
    if car.age <= 10 and car.age >= 5:
        return True
    else:
        return False

The thing is there will be lots of different lists I need to build, but a lot of the code in each of the list building functions is common. What's the best way to implement DRY in a Pythonic way here? Thanks.

Upvotes: 0

Views: 109

Answers (2)

Ski
Ski

Reputation: 14487

def filter(self, by=None):
    return [res for res in self.results if by(res)]

def get_male_owners(self):
    return self.filter(lambda res: res.owner.sex.male)

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318498

Use list comprehensions:

def get_male_owners(self):
    return [res for res in self.results if res.owner.sex.male]

def get_cards_5_to_10(self):
    return [res for res in self.results if res.car and self.is_5_to_10(res.car)]

def is_5_to_10(self, car):
    return 5 <= car.age <= 10

If you just need something iterable you could also return a generator expression by replacing the brackets with parentheses.

And yes, the x <= y <= z expression does work in python and it does yield the correct result and not something like (5 <= car.age) <= 10.

Upvotes: 3

Related Questions