Saturday, 15 August 2015

python - Insert an element at specific index in a list and return updated list -



python - Insert an element at specific index in a list and return updated list -

i have this:

>>> = [1, 2, 4] >>> print [1, 2, 4] >>> print a.insert(2, 3) none >>> print [1, 2, 3, 4] >>> b = a.insert(3, 6) >>> print b none >>> print [1, 2, 3, 6, 4] >>>

is there anyway can updated list result, instead of updating original list in place?

l.insert(index, obj) doesn't homecoming anything, updates list. ato said, can b = a[:index] + [obj] + a[index:]. however, way is:

a = [1, 2, 4] a.insert(2, 3) b = a[:]

python list python-2.7

No comments:

Post a Comment