Adding items into a list is an essential part of working with lists.
Suppose you have a list of student names, and a new student joins your class. To keep your list up to date, you would want to add the new student’s name to your list of existing class members.
That’s where the Python
insert() method comes in. The
insert() method allows you to add an element into a list at a given index position.
This tutorial will discuss, with examples, how to use the Python
insert() method to add an element to a list at a specific position.
Python Lists: A Refresher
A list is a type of data that can store one or more items in an order. For instance, a list could be used to store the names of all books in a library, or the phone numbers of everyone who has made a reservation at a restaurant.
Lists are changeable, which means that you can update their contents, add new items (also called “elements”) to the list, and remove existing items from the list.
Here is an example of a list in Python:
birds = [“Robin”, “Collared Dove”, “Great Tit”, “Goldfinch”, “Chaffinch”]
This list contains five values, each of which is a species of bird. As you can see, each item in our list is separated out using commas, and the list itself is enclosed within square brackets.
Each item in our list has its own index number, starting from 0, which we can use to access items individually in our list. So,
Robin has the index number 0,
Collared Dove has the index number 1, and so on.
Python List insert()
insert() method allows you to add an element to a list at a specific position.
The syntax for the
insert() method is as follows:
The parameters for the
insert() method are:
- index: The position where an element should be inserted.
- element: The item you want to add to your list.
This method is similar to the Python append() method, which also adds an item to a list. However, while
append() adds an item to the end of a list,
insert() adds an item to a list at a specific position.
Python List insert() Examples
Let’s suppose that our list from earlier stores an account of all the birds we have seen in our garden this year. After a birdwatching session, we have spotted a new bird: the Starling. We want to add this bird to our list, at the very start.
To do so, we could use the following code:
birds = ["Robin", "Collared Dove", "Great Tit", "Goldfinch", "Chaffinch"] birds.insert(0, "Starling") print(birds)
Our code returns:
[‘Starling’, ‘Robin’, ‘Collared Dove’, ‘Great Tit’, ‘Goldfinch’, ‘Chaffinch’]
Let’s break down our code. First, we declare an array called
birds which stores a list of the birds we have spotted.
Then, we use the
insert() method to add the
Starling to our list of birds. In this case, because we want to add our bird to the start of our list, we specify the index parameter 0. This adds the value
Starling to the start of our list.
Next, we print out the updated contents of our list using the
In the above example, we added a string to our list, but we could use the
insert() method to add any type of data instead.
Suppose we have a list that stores two lists. One of the lists stores the birds we have seen, and the other list stores the birds we want to see. We want to add a new list which lists the birds we think we have seen, but where we have had no confirmation from someone else that we have actually seen a bird.
We could add this list using the following code:
birds = [ ["Robin", "Collared Dove", "Great Tit", "Goldfinch", "Chaffinch"], ["Dunnock", "Long-tailed Tit", "Greenfinch"] ] list_to_add = ["Song Thrush", "Grey Tit"] birds.insert(1, list_to_add) print(birds)
Our code returns:
[[‘Robin’, ‘Collared Dove’, ‘Great Tit’, ‘Goldfinch’, ‘Chaffinch’], [‘Song Thrush’, ‘Grey Tit’], [‘Dunnock’, ‘Long-tailed Tit’, ‘Greenfinch’]]
Let’s break down our code. First, we declare a list called birds which stores two lists. The first list stores a list of birds that we have seen, and the other stores the list of birds we want to see.
Then, we have declared another list that stores the names of all the birds we think we have seen, but we are not sure about. Next, we use the
insert() method to add this list to the index position 1 in our
As you can see in the output of our code, the contents of the variable
list_to_add were added at index position 1, which means they appeared after the first list in our list of lists.
Similarly, you can use the
insert() method to add numbers, booleans, tuples, and other types of data into a list.
The Python insert() method allows you to add an item to a list at a specific position in that list. This tutorial discussed, with reference to examples, the basics of Python lists and how to use the insert(). Now you’re ready to start using the insert() method like a Python pro!
Are you looking to learn how to code in Python? Download the free Career Karma app today to unlock free learning resources, and to speak with a coach who can help you identify the right training program for you.