-1

I am a TA helping a student out with CS1101 and they had a very interesting question.

In python, why does it allow you to do this:

return data + [data[-1]]

But not:

return data.append(data[-1])

I know that you can do the second line with:

data.append(data[-1])
return data

But that doesn't answer my question on why the second statement doesn't work!

Thank you in advance and no this is not a homework question!

blackbox_devops
  • 13
  • 1
  • 1
  • 4

1 Answers1

3

.append does not return the list with the new item. It adds the item in place. As a result, .append returns None.

TerryA
  • 56,204
  • 11
  • 116
  • 135
  • As a test for A. S., try: data = []; print(data.append('hello')), notice you get None, as you're givng an instruction to do something to data, not to print the contents of data – Ari Mar 05 '19 at 03:12