0

A=['1']
C=A.append('1')
print C

Why is the above code return None but not ['1', '1'] in Python ?

CYC
  • 619
  • 1
  • 5
  • 13
  • 1
    The `append` function clearly doesn't work quite like you expect. Have you look at [the docs](https://docs.python.org/2/tutorial/datastructures.html)? – KChaloux Mar 05 '15 at 18:21

2 Answers2

6

The reason why you are not getting anything back is because the append method has no return value.

You could do:

    A=['1']
    C=A
    C.append('1')
    print(C)

Then you should get the right answer for your case that you are expecting to get.

AvetisCodes
  • 1,375
  • 2
  • 11
  • 31
1

In Python, the append method mutates the list it was called on but does not return it. There's not much reason to either, since you already have a reference to the list.

Eric
  • 6,965
  • 26
  • 32