How to add a new item in python list
In this article, you will learn about how to add a new item to a python list.
In other programming languages, you may have heard about arrays and adding a new item to an array. List in Python is nothing but an array and similarly, you can add a new item to a python list also. In python, there are basically a few methods for performing this action. The methods are -append(), insert() and, extend(). In this article, we will discover these methods and learn how we can add a new item to a python list by using these methods.
append() method
You can add a new item to a list by using this append() method. Basically, it adds an item to the existing list of python and simply modifies the list. That means it does not return a new list. Let’s see an example of it in the below section:
student_list = ["Alex", "Deven", "Anik"]
#student_list before add an item
print(student_list)
student_list.append("Rathore")
#student_list after add an item
print(student_list)
# Output:
# ['Alex', 'Deven', 'Anik']
# ['Alex', 'Deven', 'Anik', 'Rathore']
Here, you can see that, in our student_list at first there were three elements and after using the append() method the elements of the list increased to four, and a new element Rathore added at the end of the list.
insert() method
In the append() method, the new element will always be added at the end of the list. As a result, if we need to append an item to a particular position it will not possible with this. To perform this kind of action, we need to use the insert() method. It will do the same thing but it gives us extra power like we can select a position for adding a new item. See the below code example:
student_list = ["Alex", "Deven", "Anik"]
#student_list before add an item
print(student_list)
student_list.insert(2, "Rathore")
#student_list after add an item
print(student_list)
# Output:
# ['Alex', 'Deven', 'Anik']
# ['Alex', 'Deven', 'Rathore', 'Anik']
Here, you can see that we have selected index number 2, and our new element Rathore at position three. If you want to add a new element in a particular position then all you need to do is to use the insert() method and select the index number.
extend() method
If you need to add multiple items to a python list then the above two methods may not be suitable. But not to worry, python provides another method named extend() to perform this action. In the extend() method, you need to define a new list of items that you need to add to your existing list. Let’s see an example of it to understand things more clearly.
student_list = ["Alex", "Deven", "Anik"]
#student_list before add multiple items
print(student_list)
new_list = ["Alina", "Gabriel"]
student_list.extend(new_list)
#student_list after add multiple items
print(student_list)
# Output:
# ['Alex', 'Deven', 'Anik']
# ['Alex', 'Deven', 'Anik', 'Alina', 'Gabriel']
Here, you can see in the output that, we can add multiple items to a list with the help of the extend() method.
These are the useful methods in python for adding a new item to the list. You may follow these approaches based on your demand for the program.