+2 votes
in Programming Languages by (14.7k points)

When I use extend() function to combine two lists, it returns None. How can I combine lists?

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> f=a.extend(b)
>>> f

1 Answer

0 votes
by (25.6k points)

extend() is an in-place function, that's why f is assigned None. The list 'a' will be extended by your code. If you want to assign the extended list to 'f', you can do one of the followings:

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> f=a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> f=a
>>> f
[1, 2, 3, 4, 5, 6]
>>> import numpy as np
>>> f1=list(np.append(a,b))
>>> f1
[1, 2, 3, 4, 5, 6, 4, 5, 6]
>>>

...