Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:35:48+00:00 2026-06-08T08:35:48+00:00

In short. How do I write something else than this: for another in combinationOfK(K-1,

  • 0

In short. How do I write something else than this: for another in combinationOfK(K-1, L[i+1:]): My function combinationOfK(…) is not iterable.

I am trying to understand the code from here, solution to. Problem 26: Generate the combinations of K distinct objects chosen from the N elements of a list
I know what yield does. But I am trying to write the code without a yield statement. Code with yield statement is this.

def combination(K, L):
    if K<=0:
        yield []
        return
    for i in range(len(L)):
        thisone = L[i:i+1]
        for another in combination(K-1, L[i+1:]):
            yield thisone + another

The question, yield-keyword-explained gave me the idea that I could replace yield. The recepie they give, which is not working for me, is:

When you see a function with yield statements, apply this easy
trick to understand what will happen:

  1. Insert a line result = [] at the start of the function.
  2. Replace each yield expr with result.append(expr).
  3. Insert a line return result at the bottom of the function.
  4. Yay – no more yield statements! Read and figure out code.
  5. Revert function to original definition.

Using this to get code without yield give me this. The code is not working (the function is not iterable). What do I have to write to get this code working without yield?

def combinationOfK(K,L):
    result = []
    if K <= 0:
        result.append([])
        return
    for i in range(len(L)):
        thisone = L[i:i+1]
        for another in combinationOfK(K-1, L[i+1:]):  # the error
            result.append(thisone + another)
    return result

I am using this code to test the function,

the_list = ['a','b','c','d','e']
print list(combinationOfK(2, the_list))

raising error TypeError: 'NoneType' object is not iterable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:35:49+00:00Added an answer on June 8, 2026 at 8:35 am

    The problem is that your original code uses return in an unusual way.

    def combination(K, L):
        if K<=0:
            yield []
            return    #  <--- hmmm
    

    Most of the time you won’t see return in a generator, because you don’t often need it. Usually, generators simply “fall off” at the end; the interpreter reaches the end of the generator without encountering a return statement, and then it knows to throw StopIteration.

    In this case, the writer of the code has inserted a return statement to “hurry up” the process. When K <= 0, there’s no more work to be done, so the generator can throw StopIteration — but without the return statement, it would go into the for loop, producing incorrect results. In my opinion, a clearer way to do this would have been like so:

    def combination(K, L):
        if K<=0:
            yield []
        else:
            for i in range(len(L)):
                thisone = L[i:i+1]
                for another in combination(K-1, L[i+1:]):
                    yield thisone + another
    

    Now the conversion works as expected:

    def combination2(K, L):
        result = []
        if K <= 0:
            result.append([])
        else:
            for i in range(len(L)):
                thisone = L[i:i + 1]
                for another in combination2(K - 1, L[i + 1:]):
                    result.append(thisone + another)
        return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a short function that will let me quickly read in
If there any way to write short cut code instead of this one I
So long story short, aside from being a noob, im not sure why this
In short: Trying to write a wcf service for a winform-app that invokes a
Sometimes I write very short assembly functions like function SeniorBit(Value: LongWord): Integer; asm OR
I'm trying to write a short Wordpress JQuery for Wordpress comments that would allow
I'm trying to write a simple WPF application in C#. Coming from a primarily
Is there something like short if else = (cond) ? true : false statement,
I'm trying to create something with a flip effect tutorial from tutplus - http://active.tutsplus.com/tutorials/effects/iphone-page-transition-flash/
This code (taken from Learn You A Haskell ): main = do putStr Hey,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.