0

The following works just fine:

b = [1,]
b.append([2, 3])
# returns type "list" [1, 2, 3]

But the following doesn't:

[1,].append([2, 3])
# returns type "NoneType"

This holds true for a few of the list methods that' I've tried. Why does Python require explicit variable declaration before applying a method?

eykanal
  • 26,437
  • 19
  • 82
  • 113

2 Answers2

2

You're not reading that right. .append() returns None in both cases. In the first case, it is b that results in [1, 2, 3]. In the second, that list is created also, but you don't have access to it becaues you didn't assign a variable name to it.

eykanal
  • 26,437
  • 19
  • 82
  • 113
zondo
  • 19,901
  • 8
  • 44
  • 83
1

append returns None as its value, but alters the list. Try

b = [1,]
output = b.append([2, 3])

You'll get None for output, too.

Prune
  • 76,765
  • 14
  • 60
  • 81