The list.insert() method inserts a specific element at the specified index in Python.
The list append() method adds elements to the end of the list, whereas the insert() method allows us to add elements anywhere in the list, providing complete control over the insertion process.
The insert() method does not return a new list; instead, it modifies the existing list.
When it comes to inserting, it is not limited to just integer types; it can insert any data type, including complex objects such as lists or dictionaries.
Syntax
list.insert(index, element)
Parameters
| Argument | Description |
| index(required) | It is a position in a list where an item is to be inserted.
If it is a negative number, it will start from the back. |
| element(required) | It is an input object to insert (any type: int, str, list, etc.). |
Basic insertion
Let’s define a simple list and insert a new element at index 2. In a list, the index starts with 0.
shares = ['KEI', 'Polycab', 'RR Kabel'] shares.insert(2, 'Delton') # Inserts at index 2 print(shares) # Output: ['KEI', 'Polycab', 'Delton', 'RR Kabel']
Adding at the start
If you pass position 0, it will start inserting the element at the start of the list.
shares = ['KEI', 'Polycab', 'RR Kabel'] shares.insert(0, 'Delton') # Inserts at index 0 print(shares) # Output: ['Delton', 'KEI', 'Polycab', 'RR Kabel']
Adding at the end (index = len(list))
If you want to add an element at the end, we need to pass the index = len(list). It works similarly to the append() method.
shares = ['KEI', 'Polycab', 'RR Kabel'] shares.insert(len(shares), 'Delton') # Inserts at the end of the list print(shares) # Output: ['KEI', 'Polycab', 'RR Kabel', 'Delton']
Negative indices
Let’s add an element at the second-to-last element by passing the negative index.
shares = ['KEI', 'Polycab', 'RR Kabel'] shares.insert(-2, 'Delton') # Inserts at index -2 print(shares) # Output: ['KEI', 'Delton', 'Polycab', 'RR Kabel']
Out-of-Bounds index handling
What if the index > len(list) or index < -len(list)? How will it handle?
If the index is less than 0, it will append an element at the start of the list.
shares = ['KEI', 'Polycab', 'RR Kabel'] shares.insert(-90, 'Delton') print(shares) # Output: ['Delton', 'KEI', 'Polycab', 'RR Kabel']
If the index is greater than the length of the list, it will append the element at the end of the list.
shares = ['KEI', 'Polycab', 'RR Kabel'] shares.insert(90, 'Delton') print(shares) # Output: ['KEI', 'Polycab', 'RR Kabel', 'Delton']
Empty list
What if the input list is empty? Well, it will add the element at index 0.
empty_list = [] empty_list.insert(0, "Polycab") print(empty_list) # Output: ['Polycab']
Comparison with append() and extend()
Here is the program to differentiates between insert(), append(), and extend() methods:
nums = [1, 2, 3] nums.append(4) # Output: [1, 2, 3, 4] nums.insert(0, 0) # Output: [0, 1, 2, 3, 4] nums.extend([5, 6]) # Output: [0, 1, 2, 3, 4, 5, 6]
Inserting a Tuple (as an Element) into the List
Let’s add a tuple as a single list element to a list.
GoT1_list = ['Daenerys', 'Jon', 'Tyrion']
Friends1_tuple = ('Rachel', 'Monica', 'Phoebe')
GoT1_list.insert(2, Friends1_tuple)
print(GoT1_list)
# Output: ['Daenerys', 'Jon', ('Rachel', 'Monica', 'Phoebe'), 'Tyrion']
That’s it!




