2

Is there an alternative (even if longer) method of writing the yield part of this code? I'm not very familiar with the function and would rather keep it simple.

for i in range(0, len(lstInput) - intCount + 1):
    if intCount == 1:
        yield [lstInput[i]]
    else:
        current = lstInput[i]
        remainder = lstInput[i+1:]
        for rest in yieldLotto(remainder, intCount - 1):
            yield [current] + rest
3
  • u can use a list and append to that Commented May 27, 2014 at 4:45
  • 8
    How about instead you learn what yield does? Commented May 27, 2014 at 4:47
  • 1
    Seriously, though, yield is not complex. Just take the 20 minutes to learn it completely and you'll never worry about it again. @jwodder's SO link is perfect and even has some links to slideshows if I remember correctly. (It's how I learned yield). Commented May 27, 2014 at 4:52

1 Answer 1

3

The alternative is to either embed the loops into your calling program, or to change it to return a list. This means everything will be going into memory, though.

def foo():
    return_list = []
    for i in range(0, len(lstInput) - intCount + 1):
        if intCount == 1:
            return_list.append([lstInput[i]])
        else:
            current = lstInput[i]
            remainder = lstInput[i+1:]
            for rest in yieldLotto(remainder, intCount - 1):
                return_list.append([current] + rest)
    return return_list

Honestly though, I think yield is better and it's an important feature of Python. I suggest learning it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.