-1

when trying to do that it is returning None.

x = [1,2,3].extend([4,5,6])

but [1,2,3] + [4,5,6] this is working fine can anybody tell me why?

I mean extend() function takes the same format, so why is it returning none?

martineau
  • 112,593
  • 23
  • 157
  • 280
Ankit Kumar Namdeo
  • 1,347
  • 1
  • 10
  • 23
  • `extend` alters the list it is called on in place, and returns `None`. If you want something like `[1,2,3] + [4,5,6]` then use that. – khelwood Feb 15 '17 at 09:36
  • `x = [1, 2, 3]` then `x.extend(4, 5, 6)` will give you the result. – ᴀʀᴍᴀɴ Feb 15 '17 at 09:36
  • `list.extend()` takes other iterables to, while concatenation doesn't. `list.extend()` updates the list object *in place* and returns `None`, do not use it on a literal. – Martijn Pieters Feb 15 '17 at 09:36

1 Answers1

2

help(list.extend) will give you something like below:

extend(...)
    L.extend(iterable) -> None -- extend list by appending elements from the iterable

So, extend do merge two list, but return None, as it is an in-place operation. For example:

>>> a = [1,2,3]
>>> print(a.extend([4,5,6]))
None
>>> a
[1, 2, 3, 4, 5, 6]
Ahsanul Haque
  • 9,865
  • 4
  • 35
  • 51