-1

When I tried something in Python, I encountered something strange:

a = [[1,2], [3,4]]

b = [x.append(5) for x in a]

It turns out b is [None, None] rather than [[1,2,5], [3,4,5]]. I think it's related to the fact list is mutable, but not quite sure what exactly happens in this case. Could any of you explain? Thank you.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Coiacy
  • 1
  • `list.append` returns `None`. You can check this for yourself: `print([].append(3))` – juanpa.arrivillaga Oct 04 '18 at 23:04
  • Note, by *convention* mutator methods on mutable types in the standard library do not return `None`, but that is not a language guarantee, and it isn't even a strict convention, note `my_list.pop()` is a mutator method and it doesn't return `None` – juanpa.arrivillaga Oct 04 '18 at 23:05

1 Answers1

1

append always returns None; you call it solely for its side effect on the object (in this case, x).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101