0

I am trying to understand why printing a list that was assigned using another list will give None. The code is as follows.

A=[]
for i in range(12,4,-2):
    A.append(i)
A_A=A.append(4)
print(A)
print(A_A)

In above code, why print(A_A) gives None?.

Thank you.

2 Answers2

1

Append does not return anything, it just modifies the original list.

Sergiu
  • 130
  • 6
1

The function

list.append()

modifies the list that it's called on. It doesn't return anything - thus, when you try to assign its return value to A_A, you get None.

You'll notice that, in your code, 4 was successfully appended to A, as you'd expect. The rationale here is, if you already know what you're trying to add, why bother returning it? Or, put another way - what could list.append() possibly return that would be useful? The conclusion was that it couldn't; thus, list.append() doesn't return anything.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53