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

305,869

Solution 1

The shortest I got: b = a[:2] + [3] + a[2:]

>>>
>>> a = [1, 2, 4]
>>> print a
[1, 2, 4]
>>> b = a[:2] + [3] + a[2:]
>>> print a
[1, 2, 4]
>>> print b
[1, 2, 3, 4]

Solution 2

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)

Solution 3

Most performance efficient approach

You may also insert the element using the slice indexing in the list. For example:

>>> a = [1, 2, 4]
>>> insert_at = 2  # Index at which you want to insert item

>>> b = a[:]   # Created copy of list "a" as "b".
               # Skip this step if you are ok with modifying the original list

>>> b[insert_at:insert_at] = [3]  # Insert "3" within "b"
>>> b
[1, 2, 3, 4]

For inserting multiple elements together at a given index, all you need to do is to use a list of multiple elements that you want to insert. For example:

>>> a = [1, 2, 4]
>>> insert_at = 2   # Index starting from which multiple elements will be inserted

# List of elements that you want to insert together at "index_at" (above) position
>>> insert_elements = [3, 5, 6]

>>> a[insert_at:insert_at] = insert_elements
>>> a   # [3, 5, 6] are inserted together in `a` starting at index "2"
[1, 2, 3, 5, 6, 4]

To know more about slice indexing, you can refer: Understanding slice notation.

Note: In Python 3.x, difference of performance between slice indexing and list.index(...) is significantly reduced and both are almost equivalent. However, in Python 2.x, this difference is quite noticeable. I have shared performance comparisons later in this answer.


Alternative using list comprehension (but very slow in terms of performance):

As an alternative, it can be achieved using list comprehension with enumerate too. (But please don't do it this way. It is just for illustration):

>>> a = [1, 2, 4]
>>> insert_at = 2

>>> b = [y for i, x in enumerate(a) for y in ((3, x) if i == insert_at else (x, ))]
>>> b
[1, 2, 3, 4]

Performance comparison of all solutions

Here's the timeit comparison of all the answers with list of 1000 elements on Python 3.9.1 and Python 2.7.16. Answers are listed in the order of performance for both the Python versions.

Python 3.9.1

  1. My answer using sliced insertion - Fastest ( 2.25 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]"
    100000 loops, best of 5: 2.25 µsec per loop
    
  2. Rushy Panchal's answer with most votes using list.insert(...)- Second (2.33 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)"
    100000 loops, best of 5: 2.33 µsec per loop
    
  3. ATOzTOA's accepted answer based on merge of sliced lists - Third (5.01 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]"
    50000 loops, best of 5: 5.01 µsec per loop
    
  4. My answer with List Comprehension and enumerate - Fourth (very slow with 135 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
    2000 loops, best of 5: 135 µsec per loop
    

Python 2.7.16

  1. My answer using sliced insertion - Fastest (2.09 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]"
    100000 loops, best of 3: 2.09 µsec per loop
    
  2. Rushy Panchal's answer with most votes using list.insert(...)- Second (2.36 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)"
    100000 loops, best of 3: 2.36 µsec per loop
    
  3. ATOzTOA's accepted answer based on merge of sliced lists - Third (4.44 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]"
    100000 loops, best of 3: 4.44 µsec per loop
    
  4. My answer with List Comprehension and enumerate - Fourth (very slow with 103 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
    10000 loops, best of 3: 103 µsec per loop
    

Solution 4

The cleanest approach is to copy the list and then insert the object into the copy. On Python 3 this can be done via list.copy:

new = old.copy()
new.insert(index, value)

On Python 2 copying the list can be achieved via new = old[:] (this also works on Python 3).

In terms of performance there is no difference to other proposed methods:

$ python --version
Python 3.8.1
$ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b.insert(500, 3)"
100000 loops, best of 5: 2.84 µsec per loop
$ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b[500:500] = (3,)"
100000 loops, best of 5: 2.76 µsec per loop

Solution 5

Here is the way to add a single item, a single item in specific index concatenate list with another list

>>> expences = [2200, 2350, 2600, 2130, 2190]
>>> expences.append(1980)
>>> expences
[2200, 2350, 2600, 2130, 2190, 1980]

>>> expences.insert(1, 1200)

>>> expences
[2200, 1200, 2350, 2600, 2130, 2190, 1980]

>>> newElm = [2550, 2123, 2430]
>>> expences.extend(newElm)
>>> expences
[2200, 1200, 2350, 2600, 2130, 2190, 1980, 2550, 2123, 2430]
>>> 
Share:
305,869

Related videos on Youtube

ATOzTOA
Author by

ATOzTOA

Programming is my passion... any programming language, you name it, I have used it... well except SmallTalk... Site: www.atoztoa.info Email: [email protected] SOreadytohelp

Updated on October 04, 2021

Comments

  • ATOzTOA
    ATOzTOA over 2 years

    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?

    • mkoistinen
      mkoistinen almost 10 years
      b = a[:].insert(2,3) seems pretty short, doesn't affect the original list and is pretty descriptive.
    • SparkAndShine
      SparkAndShine over 8 years
      @mkoistinen It doesn't work for me. >>> a = [1, 2, 3, 4] >>> b = a[:].insert(2, 5) >>> print b None
  • IceArdor
    IceArdor almost 10 years
    If you can't tolerate 3 lines of readable code, put it in a function and call it.
  • Gustav Bertram
    Gustav Bertram over 6 years
    This does not answer the question.
  • Laszlowaty
    Laszlowaty over 6 years
    Question was specific: Is there anyway I can get the updated list as result, instead of updating the original list in place? Your answer does the opposite.
  • minillinim
    minillinim over 6 years
    I really like this result because it easily extends to solve the problem, what if I want to insert the values 3, 3.5 into that list (in order) -> a[2:2] = [3,3.5]. Very neat
  • a_guest
    a_guest almost 4 years
    Number of lines of code is not a good measure of code quality. This approach is flawed for both performance and readability reasons.
  • alper
    alper over 3 years
    can we do a= a[:2] + [3] + a[2:]?