How to use Python insert to insert elements in a list

Python comes with a whole set of built-in methods that simplify and speed up the implementation of your program. These include the Python insert method, which lets you insert a single element into an existing Python list. To add multiple elements to the list at once or append single elements at the end, the Python extend and Python append methods are useful.

Syntax, parameters and function of Python insert

The insert method in Python requires two parameters, which are the element to be inserted and the index indicating where the element should be placed. Python lists support a variety of data types, allowing the element parameter to hold any type of data (such as a list, integer, etc.). However, it’s important to note that the index parameter must always be an integer. The following code snippet demonstrates the insert syntax:

ingredients.insert(1, "sugar")
Python

In this example, the string "sugar" is inserted at position 1 in the list of ingredients. The index where the element (the string) is to be inserted is passed as the first argument, the element itself as the second. As usual, both parameters may either be passed on directly or created as variables before the method is called up. The following example illustrates how the method works:

ingredients = ["flour", "eggs", "butter"]
ingredients.insert(0, "sugar")
print(ingredients) # output: ['sugar', 'flour', 'eggs', 'butter']
Python

In this example, a list of baking ingredients is expanded by adding two more ingredients, which are inserted at indices 0 and 2. It is crucial to remember that indexing in Python begins at 0, as is customary in many programming languages. This implies that the first element has an index of 0, the second element has an index of 1, and so on. When an element is inserted into a list using the insert() method, the existing elements at the specified index and all subsequent elements are shifted one position to the right in order to accommodate the new element.

If the given index is beyond the valid range of the list (e.g. larger than the number of elements in the list), the element is inserted at the end of the list. In this case, insert() behaves identically to the Python append method. However, if a negative index is specified, it’s interpreted as the distance from the end of the list. For instance, -1 corresponds to the second-to-last element, -2 represents the element before that, and so on. The following example demonstrates these two properties:

ingredients.insert(10, "baking powder")
print(ingredients) # output: ['sugar', 'flour', 'eggs', 'butter', 'baking powder']
ingredients.insert(-2, "salt")
print(ingredients) # output: ['sugar', 'flour', 'eggs', 'salt', 'butter', 'baking powder']
Python
Tip

Looking for a platform that helps you publish your website or web app quickly and easily? Deploy Now from IONOS is the right choice for you!

Alternatives to Python insert

Depending on the behaviour you’re after, there are some Python operators that are suitable alternatives to insert(). Here are two of them.

The index operator

The index operator can be used to read or overwrite an element at a specified index in a list. Thus, with the index operator it’s possible to replace one element in a list with another. This is illustrated in the following example:

print(ingredients) # output: ['sugar', 'flour', 'eggs', 'salt', 'butter', 'baking powder']
ingredients[2] = "baking soda"]
print(ingredients) # output: ['sugar', 'flour', 'baking soda', 'salt', 'butter', 'baking powder']
Python

The slice operator

However, if you don’t want to delete elements from your list, or insert several elements to the middle of your list, the slice operator is a better choice. Unlike the index operator, the slice operator returns a subsequence of the list rather than a single element. For instance, if we have a list named ingredients, we can use the slice operator to obtain a subsequence of elements from indices 3 to 5, resulting in ['salt', 'butter', 'baking powder']. The following example illustrates using the slice operator to insert any number of elements in the middle of a list:

to add = ["More sugar", "Even more sugar"]
ingredients = ingredients[:3] + add + ingredients[3:]
print(ingredients)
Python

In this example, the list add is inserted in the middle of the ingredients list. The process involves using the slice operator to extract all the elements from the ingredients list up to index 3 and concatenating them with the add list. Then, we take all the elements from the ingredients list starting from index 3 and concatenate them with the resulting list. Finally, the ingredients list is overwritten with the list obtained from this double concatenation.

It’s important to note that this solution requires many writes, since the whole list is overwritten. This should be considered if you want your program to be very efficient and you work with very large lists.

Was this article helpful?
Page top