0

I am trying to create custom permutations in a list (mainly to mess around with recursion in python). Right now I get this error when I run my code:

TypeError: 'NoneType' object is not iterable

Before I added in the list copying, I was getting this:

AttributeError: 'NoneType' object has no attribute 'append'

def findPermutations (size, max, curr_perm):
    if not curr_perm or len(curr_perm) < size:
        for i in range(1,max):
            new_arr = list(curr_perm)
            findPermutations(size, max, new_arr.append(i))
    else:
        return curr_perm


print(findPermutations(2,3,[]))

I was hoping to return a bunch or permutations. What am I doing wrong here?

2
  • NoneType means you're trying to access something that doesn't exist. In other languages known as null. Use a debugger and check the values. Commented Apr 1, 2019 at 16:24
  • 2
    new_arr.append(i) returns None. Append, then pass the list into the function Commented Apr 1, 2019 at 16:25

3 Answers 3

1

You need append the item list before calling recursive function. Below is the working code, Please let me know if you have any questions, i would be very happy to help you out.

def findPermutations (size, max, curr_perm):
    if not curr_perm or len(curr_perm) < size:
        for i in range(1,max):
            new_arr = list(curr_perm)
            new_arr.append(i)
            print(new_arr)
            findPermutations(size, max, new_arr)
    else:
        return curr_perm
findPermutations(2,3,[])
**Result:**
[1]
[1, 1]
[1, 2]
[2]
[2, 1]
[2, 2]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! First: can you explain why having it in the function call didn't work? Second: how do I get the function itself to return the lists; it still returns None?
Here two things we need to consider: The list.append function does not return any value(but None), it just add the value to the list you are using to call that method and other one is python lists runs asynchronously until we specify. so in your case when you call new_arr.append(i) in function call, by the time, it considers list is empty before executing append in function call. For your second question, did you tried with my solution. I don't think you will see 'NONE' with my solution. let me know if you still have questions.
That makes sense, thanks. I have it working now, I didn't see that you had put in the print statement. Thanks again!
Glad you it helped. Could you please mark this as correct answer.
0

To solve your code:

    new_arr.append(i)
    findPermutations(size, max, new_arr)

1 Comment

Thanks! Why do they need to be split up?
0

The list.append() function returns None.

It does not return the new updated list. It simply modifies the existing list and returns None. Hence None is passed as the third parameter in the findPermutations() function in the subsequent recursive call.

The answer by @Wonka splits up the code into two parts. This allows the new_arr list to be updated first and then passed onto the function.

Using new_arr + [i] would be a better option here. This creates a new list and that is passed onto the next recursive function call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.