-3

I was experimenting with another question on Stack. I was trying to extend the list aa so that it contain all elements of complete. However, I came across this error. The following print statement prints None:

complete = [5,4,3,2,1]
aa = [1, 2]
aa = aa.extend(complete)
print aa # prints None

However, if I change the code a bit like so:

complete = [5,4,3,2,1]
aa = [1, 2]
aa.extend(complete)
print aa # prints [1, 2, 5, 4, 3, 2, 1]

it works just fine. Why is it printing None in the above code?

Abhinav
  • 9
  • 2
  • `extend()` is an in-place method that returns None. Check out the documentation – Peter Wang Aug 09 '16 at 18:53
  • @PeterWang Got it. I am extremely sorry for asking a duplicate. Although I did search it up, I was not able to find that post for some reason. I was also trying to make the question as clear as possible. Sorry. – Abhinav Aug 09 '16 at 18:55

1 Answers1

2

The extend method does not return any value, it simply modifies the list in place. Since you assign the return value of extend, which is None, to aa… well, it becomes None.

deceze
  • 510,633
  • 85
  • 743
  • 889