Python List insert() Method with Examples

In the previous article, we have discussed Python List copy() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List insert() Method in Python:

Inserts the specified value at the specified position using the insert() method.

Syntax:

list.insert(position, element)

Parameters

position: This is Required. A number indicating where the value should be inserted.

element: This is Required. Any type of element (string, number, object etc.)

Return Value:

The insert() method produces no results; it returns None. It only makes changes to the current list.

Examples:

Example1:

Input:

Given List = ["hello", "is", "btechgeeks"]
Given position = 1
Given element = "this"

Output:

The given list after inserting the given element: 
['hello', 'this', 'is', 'btechgeeks']

Example2:

Input:

Given List = ["good", "morning", "btechgeeks"]
Given position = 1
Given Tuple = (1, 3, 5, 7)

Output:

The given list after inserting the given Tuple: 
['good', (1, 3, 5, 7), 'morning', 'btechgeeks']

List insert() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1) Inserting an element to the List

Approach:

  • Give the list as static input and store it in a variable.
  • Give the position as static input and store it in another variable.
  • Give the element as static input and store it in another variable.
  • Pass the given position and given element as arguments to the insert() function for the given list to insert the given element at the given position.
  • Print the given list after inserting the given element.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "is", "btechgeeks"]
# Give the position as static input and store it in another variable.
gvn_positin = 1
# Give the element as static input and store it in another variable.
gvn_elemnt = "this"
# Pass the given position and given element as arguments to the insert()
# function for the given list to insert the given element at the given position.
gvn_lst.insert(gvn_positin, gvn_elemnt)
# Print the given list after inserting the given element.
print("The given list after inserting the given element: ")
print(gvn_lst)

Output:

The given list after inserting the given element: 
['hello', 'this', 'is', 'btechgeeks']
2)Inserting tuple as an element to the List

Approach:

  • Give the list as static input and store it in a variable.
  • Give the position as static input and store it in another variable.
  • Give the tuple as static input and store it in another variable.
  • Pass the given position and given tuple as arguments to the insert() function for the given list to insert the given tuple at the given position.
  • Print the given list after inserting the given tuple.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["good", "morning", "btechgeeks"]
# Give the position as static input and store it in another variable.
gvn_positin = 1
# Give the tuple as static input and store it in another variable.
gvn_tupl = (1, 3, 5, 7)
# Pass the given position and given tuple as arguments to the insert()
# function for the given list to insert the given tuple at the given position.
gvn_lst.insert(gvn_positin, gvn_tupl)
# Print the given list after inserting the given Tuple.
print("The given list after inserting the given Tuple: ")
print(gvn_lst)

Output:

The given list after inserting the given Tuple: 
['good', (1, 3, 5, 7), 'morning', 'btechgeeks']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the position as user input using the int(input()) function and store it in another variable.
  • Give the element as user input using the input() function and store it in another variable.
  • Pass the given position and given element as arguments to the insert() function for the given list to insert the given element at the given position.
  • Print the given list after inserting the given element.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the position as user input using the int(input()) function and 
# store it in another variable.
gvn_positin = int(input("Enter some random number = "))
# Give the element as user input using the input() function and 
# store it in another variable.
gvn_elemnt = input("Enter some random string = ")
# Pass the given position and given element as arguments to the insert()
# function for the given list to insert the given element at the given position.
gvn_lst.insert(gvn_positin, gvn_elemnt)
# Print the given list after inserting the given element.
print("The given list after inserting the given element: ")
print(gvn_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40 70
Enter some random number = 2
Enter some random string = hello
The given list after inserting the given element: 
[10, 20, 'hello', 30, 40, 70]

Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples and use them.