I have this:
>>> a = [1, 2, 4]
>>> print a
[1, 2, 4]
>>> print a.insert(2, 3)
None
>>> print a
[1, 2, 3, 4]
>>> b = a.insert(3, 6)
>>> print b
None
>>> print a
[1, 2, 3, 6, 4]
Is there a way I can get the updated list as the result, instead of updating the original list in place?
l.insert(index, obj)
doesn't actually return anything. It just updates the list.
As ATO said, you can do b = a[:index] + [obj] + a[index:]
.
However, another way is:
a = [1, 2, 4]
b = a[:]
b.insert(2, 3)