Python comes with a whole set of built-in methods that simplify and speed up the im­ple­ment­a­tion 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, para­met­ers and function of Python insert

The insert method in Python requires two para­met­ers, which are the element to be inserted and the index in­dic­at­ing 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 demon­strates the insert syntax:

ingredients.insert(1, "sugar")
Python

In this example, the string "sugar" is inserted at position 1 in the list of in­gredi­ents. 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 para­met­ers may either be passed on directly or created as variables before the method is called up. The following example il­lus­trates 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 in­gredi­ents is expanded by adding two more in­gredi­ents, 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 pro­gram­ming 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 sub­sequent elements are shifted one position to the right in order to ac­com­mod­ate 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 identic­ally to the Python append method. However, if a negative index is specified, it’s in­ter­preted as the distance from the end of the list. For instance, -1 cor­res­ponds to the second-to-last element, -2 rep­res­ents the element before that, and so on. The following example demon­strates these two prop­er­ties:

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!

Al­tern­at­ives to Python insert

Depending on the behaviour you’re after, there are some Python operators that are suitable al­tern­at­ives 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 il­lus­trated 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 sub­sequence 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 sub­sequence of elements from indices 3 to 5, resulting in ['salt', 'butter', 'baking powder']. The following example il­lus­trates 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)
# Output: ['Sugar', 'Flour', 'Baking Soda', 'More Sugar', 'More Sugar', 'Salt', 'Butter', 'Baking Powder']
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 con­cat­en­at­ing them with the add list. Then, we take all the elements from the ingredients list starting from index 3 and con­cat­en­ate them with the resulting list. Finally, the ingredients list is over­writ­ten with the list obtained from this double con­cat­en­a­tion.

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

Go to Main Menu