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

  • SEARCH
  • Home
  • 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 1003259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:57:10+00:00 2026-05-16T07:57:10+00:00

As an exercise, I’ve been trying out various ways of generating all permutations of

  • 0

As an exercise, I’ve been trying out various ways of generating all permutations of a list in Python — recursive, non-recursive… — and comparing the performance with itertools.permutations(). But I’m having trouble with the generator version of the recursive method, which doesn’t finish cleanly with a StopIteration exception, but instead throws an IndexError:

def spawnperms(alist):
    """same algorithm as recursive option, but a generator"""
    if (alist == []):
        yield []
    for perm in spawnperms(alist[:-1]):
        for i in range(len(perm)+1):
            yield perm[:i] + [alist[-1]] + perm[i:]

Calling this from the Python interpreter:

>>> for i in spawnperms(range(3)):
...     print i
... 
[2, 1, 0]
[1, 2, 0]
[1, 0, 2]
[2, 0, 1]
[0, 2, 1]
[0, 1, 2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in spawnperms
  File "<stdin>", line 5, in spawnperms
  File "<stdin>", line 5, in spawnperms
  File "<stdin>", line 7, in spawnperms
IndexError: list index out of range

Ouch. I tried stepping through it with pdb, which nearly created a stack overflow in my brain, but what I understood is that the recursion “reaches down” to the empty list, and then the outer (I think) for loop runs out of indices.

How can I correct my code?

EDIT: One learning from Mark Byers’ deceptively simple correct answer is that clean coding practices can prevent mistakes. Had I used an else systematically after if, regardless of whether I thought the condition could ever be revisited, this wouldn’t have happened. And it still feels very stupid!

  • 1 1 Answer
  • 1 View
  • 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-05-16T07:57:11+00:00Added an answer on May 16, 2026 at 7:57 am

    You are missing an else:

    if (alist == []):
        yield []
    else:
        for ...
    

    This is because yield does not behave in the same way as return. Execution continues after the yield statement when you request the next value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As an exercise I'm trying to create a function that returns a generic list
As an exercise for myself, I was translating a sample program into various languages.
I do more and more exercise with Lambda but I do not figure out
I've been working through Practical Common Lisp and as an exercise decided to write
Trying to do exercise 1.16 (iterative version of fast-exp) in Structure and Interpretation of
As an exercise, I'm trying to create a input stream manipulator that will suck
So as a learning exercise, I am trying to make a simple file browser
How do I exercise an action to ensure it redirects to the correct action
This is an exercise for the CS guys to shine with the theory. Imagine
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates

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.