In the code below, I have defined 3 functions for each of the three ways to concatenate 2 lists. Note that func1 and func3 , both modify list1 when you print it after calling either of the functions. While, when you call func2, and then you print list1, it remains the same as before.
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
def func1(a_list, b_list):
(a_list.extend(b_list))
return(a_list)
def func2(a_list, b_list):
a_list = a_list + b_list
return (a_list)
def func3(a_list, b_list):
a_list += b_list
return (a_list)
print(func2(list1, list2)) #does not change list1
print(list1) # list1 is different from return value of func3
But with any of the calls below:
print(func3(list1, list2))
print(list1) #list1 is same as the return value of func3
print(func1(list1, list2))
print(list1) #list1 is same as the return value of func1
+
operator gives the result in another list, it's not an in-place addition. Whereas, extend is an in-place modification to the list.. The usage should be primarily on your logic, and probably on the size of your list...