-1

I have two lists given below

l1=[1,2,3,4]
l2=[5,6,7,8]

If i perform l1.extend(l2) and print(l1), output is [1,2,3,4,5,6,7,8] as expected but if i assign it to a variable like b=l1.extend(l2) and print(b) the output is None.

Can anyone explain this why it is so ?

Sayse
  • 42,633
  • 14
  • 77
  • 146
Anil
  • 33
  • 7

2 Answers2

1

extend() works in place and returns None so you get the output of l1.extend(l2) returned as None.

Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
1

The reason is that l1.extend updates the object itself (l1) and doesn't return anything.

From Python docs:

list.extend(iterable)
Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
Shahar Glazner
  • 377
  • 4
  • 10