I want to attach a list to itself and I thought this would work:
x = [1,2]
y = x.extend(x)
print y
I wanted to get back [1,2,1,2]
but all I get back is the builtin None
. What am I doing wrong? I'm using Python v2.6
I want to attach a list to itself and I thought this would work:
x = [1,2]
y = x.extend(x)
print y
I wanted to get back [1,2,1,2]
but all I get back is the builtin None
. What am I doing wrong? I'm using Python v2.6
x.extend(x) will extend x inplace.
x.extend(x)
does not return a new copy, it modifies the list itself.Just print
x
instead.You can also go with
x + x
or just:
x.extend(x)
modifiesx
in-place.If you want a new, different list, use
y = x + x
.If you want a new copy of the list try:
The difference is that
extend
modifies the list "in place", meaning it will always returnNone
even though the list is modified.