Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Agreed these are a little verbose but they get the job done no?

    for n in filter(is_even, range(100)):
        print(f'{n} is odd number')

    for n in (i for i in range(100) if i % 2 == 0):
        print(f'{n} is odd number')
Are there any points against these solutions other than verbosity?


Yes, that's what I've been using so far, especially filter, which works quite well with lambda. But if you have a separate function anyway it's better to make it into a generator:

    def odd_range(count):
        return (x for x in range(count) if x%2)
        
    for n in odd_range(100):
        ...
As for the second one, I'm just not too happy with the implied two loops (even if it amounts to only one in practice).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: