Why does `list.append()` return `none`?

This is the function I used here, it returned ‘None’. But why is it wrong? Why do I need a second line to return new_lst, why can’t I combine it in one step?
I feel like I’m missing(or forgot) some basic knowledge which would make this clear to me.

def exponents(bases, powers):
new_lst =
for base in bases:
for power in powers:
return new_lst.append(base ** power)

print(exponents([2, 3, 4], [1, 2, 3]))

Next time try using the </> symbol for code so that it keeps the indentation.

I think this is what you have?

def exponents(bases, powers):
  new_lst = []
  for base in bases:
    for power in powers:
      return new_lst.append(base ** power)

print(exponents([2, 3, 4], [1, 2, 3]))

.append() is a method which performs an action toward new_lst and doesn’t return anything.

I think you want to .append() the list after the powers in powers loop (where it is at now) but not return it. At the end after the list is created (after both for loops) then return the new_lst .

Here is an example of what you tried to do:

x = []
y = x.append(2)



>>> [1, 2]
>>> None

See how y returns None? That is because .append() doesn’t return a value, but it still performed the action to append x. That’s kind of what you tried to do in your code.


Thank you for the tip, I didn’t realise my code would be copied as flat text, but you did get the code right!

And thanks for the example, I do understand that one. In your example y gets ‘None’ because the 2 get appended to x, leaving y without any value, right?
So for mine, I first have to state new_lst.append() before I can return it. If I don’t state that first, it doesn’t have anything to return yet, since it was getting appended after I typed return. Is that making sense?

No, not really. .append() performs an action on an already existing list. It modifies the list in place instead of of returning a new list. You’re .append() is acting on the list you specify to make the addition to it.

Here’s a hopefully not confusing analogy. It’s like having a basket with one red apple. When we say basket.append(green apple) it’s an action that places a green apple in the basket. It doesn’t create a new basket, it doesn’t report what’s now in the basket. It’s purely the action. It’s putting that green apple in the basket.

So when you ask the action what it physically is or ask the action to report itself. It isn’t physically anything (isn’t data) . Thus it is None. It’s not a physical thing that reports or returns anything. It’s simply the action only. You can call the basket and see what is in after the action ( one red apple and one green apple) it because it is physical thing or actual data in the case above with the lesson.

That’s how I understand it.


Ooooh! Now I see, your analogy was really clear!!

I can’t return the basket.append(green apple). But after the green apple is put in there, I can return the basket to see what is in there now. I was trying to do it during the action, but I needed to look inside the basket afterwards, and not at the action itself.


Something else of note that is not immediately obvious, list.append() has no return, so when we,

return my_list.append()

all that is seen by the caller is nothing.

>>> def add_to_list(my_list=[]):
	return my_list.append('a')

>>> add_to_list()
1 Like

Thank you monochromaticmau for your explanation and thank you Roy, it was so simple. I didn’t think that there was a difference to declare variable inside or outside the loop. Now, it’s clear for me. I was confused because I tested my fuction with IDLE and also http://pythontutor.com/visualize.html#mode=edit
and in both cases, they didn’t show me the mistake. The result was good…so, it was not easy to understand. Thank you guys !