-1

I'm studying python, and it comes to me:

>>> a[0] = [1].append("test")
>>> a
[None, 2, 3, 4]

Expanding...

>>> a = [1,2,3,4]
>>> a[0]
1
>>> a[0] = [a[0]]
>>> a
[[1], 2, 3, 4]
>>> a[0].append(2)
>>> a
[[1, 2], 2, 3, 4]
>>> a[0].append("Batata")
>>> a
[[1, 2, 'Batata'], 2, 3, 4]

Why the short sentence [1].append("test") does not work?

KpsLok
  • 853
  • 9
  • 23

1 Answers1

1

append() method works in-place and returns None.

So, if you do the following, b will have the value None:

a = []
b = a.append("test")
print(b)
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35