CodeBlessU
learn code with examples

Python Insert element

Use .insert() method to add item at specific index in mutable sequence (List / Array / String). Index of existing elements (from specified index onwards) will increase by one step. See How to insert to tuple (immutable).

Both arguments are mandatory.

If specified index is higher than n-1 (last), it inserts element at last. If specified index is lower than -n (first), it will insert element at start.

Syntax:
variable.insert(index, new_item)
Return Value:
no return value
Use
  • Replace/Update - to update value at existing position.
  • Append - to add new element at end (Single).
  • Extend - to add new elements at end from iterable source (Single / Multiple).
  • Insert - to insert new element at start / middle (Single).

TypeError: insert() takes exactly 2 arguments

Why this error:

Are you trying to call .insert() with less than two or more than two arguments?

Solution:

Call .insert() with two arguments - index, new_item

List insert

List insert in middle

# Example, List insert in middle position
a1 = [10, 20, 30, 40, 50]

a1.insert(1, 15)
print(a1) # [10, 15, 20, 30, 40, 50]

List insert at start / front

# Example, List insert at start / front
a1 = [10, 20, 30, 40, 50]

a1.insert(0, 1000)
print(a1) # [1000, 10, 20, 30, 40, 50]

List insert at end

Alternatively .append() can be used.

# Example, List insert at end
a1 = [10, 20, 30, 40, 50]

a1.insert(6, 1000)
print(a1) # [10, 20, 30, 40, 50, 1000]

List insert index is outside possible range

# Example, List insert index is outside possible range
a1 = [10, 20, 30, 40, 50]

a1.insert(15, 15) # list contains 5 element, index value specified is 15
print(a1) # [10, 20, 30, 40, 50, 15]

a1.insert(-15, -15) # 
print(a1) # [-15, 10, 20, 30, 40, 50, 15]

TypeError: insert() takes exactly 2 arguments

# Example, list insert raises TypeError
a1 = [10, 20, 30, 40, 50]

a1.insert() # raises TypeError: insert() takes exactly 2 arguments

Tuple insert

Tuple once created, can not be modified. To insert element, slicing and concatenation is used.

# Example, insert element at specific position in tuple
a1 = (1, 10, 20, 30, 50, 100)

# insert 25 at index 3
a1 = a1[:3] + (25,) + a1[3:]
print(a1)

Array insert

Array insert in middle

# Example, Array insert in middle position
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.insert(1, 15)
print(a1) # array('l', [10, 15, 20, 30, 40, 50])

Array insert at start / front

# Example, Array insert at start / front
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.insert(0, 1000)
print(a1) # array('l', [1000, 10, 20, 30, 40, 50])

Array insert at end

Alternatively .append() can be used.

# Example, Array insert at end
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.insert(6, 1000)
print(a1) # array('l', [10, 20, 30, 40, 50, 1000])

Array insert index is outside possible range

# Example, Array insert index is outside possible range
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.insert(15, 15) # Array contains 5 element, index value specified is 15
print(a1) # array('l', [10, 20, 30, 40, 50, 15])

a1.insert(-15, -15) # 
print(a1) # array('l', [-15, 10, 20, 30, 40, 50, 15])

TypeError: insert() takes exactly 2 arguments

# Example, Array insert raises TypeError
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.insert() # raises TypeError: insert() takes exactly 2 arguments

In this page...

Copyright © All rights reserved.