Extend vs += Behaviour

Question:

Help me understand the problem I am failing to identify in the following code.

def extend_a_list(list_var):
    return list_var.extend([6, 2])

def plus_equals_list(list_var):
    list_var += [6]
    return list_var

list_var = [1, 2, 3]
print "Extending"
print extend_a_list(list_var)

list_var = [1, 2, 3]
print "Plus Equals"
print plus_equals_list(list_var)


>>> Extending
>>> None
>>> Plus Equals
>>> [1, 2, 3, 6]

Extend is giving None. Why is that?

Asked By: hlkstuv_23900

||

Answers:

Extend returns nothing, it modifies the inputted list:

a = [1,2,3]
# a = [1,2,3]
b = a.extend([4])
# a = [1,2,3,4]
# b = None
Answered By: Mathias711
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.