In this Python tutorial, we will discuss, how to insert item at end of Python list. We will see 4 different ways to add item to end of list python.
A list of items will be used as examples throughout the course to focus on list insertion because the list insertion technique must be the same regardless of the type of data the list contains. Let us see different ways to insert item at end of Python list.
Insert Item At End of Python List
Here let us see 4 different ways to insert item at end of Python list.
Method 1: Insert Item At End of Python List using insert() function
Python’s insert() function for lists that inserts a specified element or item at a specified index. The syntax is given below.
list_name.insert(index, value)
If the specified index is greater than or equal to the length of the list, the insert is made at the end of the Python list.
Create a list of items that is the real GDP percentage of the top 4 states in the USA such as Tennessee = 8.6
, New Hemsphire = 8.5
, California = 7.8
and Nevada = 7.1
using the below code.
usa_st_gdp = [8.6, 8.5, 7.8, 7.1]
print(usa_st_gdp)
Now we want to add the real GDP percentage of the new state Indiana
or it is going to the 5th state in the list, we will use the insert()
method.
usa_st_gdp.insert(len(usa_st_gdp), 6.9)
Check the list using the below code.
print(usa_st_gdp)
Look in the output, how we have inserted the item which floats value at the end of the list in Python.
Read Python program to print element in an array
Method 2: Insert an item at end of a list in Python using append() function
Create a list of items that is the real GDP percentage of the top 4 states in the USA such as Tennessee = 8.6
, New Hemsphire = 8.5
, California = 7.8
and Nevada = 7.1
using the below code.
usa_st_gdp = [8.6, 8.5, 7.8, 7.1]
print(usa_st_gdp)
We will now use the append() method to add the real GDP % of the new state Indiana
or the state that will become the fifth state in the list.
usa_st_gdp.append(6.9)
View the list using the below code.
print(usa_st_gdp)
This is how to insert an item at end of a list in Python using append() function.
Read Python Palindrome Program With Examples
Method-3: Insert an item at end of a list in Python using + and [] Operator
Create a list of items that is the real GDP percentage of the top 4 states in the USA such as Tennessee = 8.6
, New Hemsphire = 8.5
, California = 7.8
and Nevada = 7.1
using the below code.
usa_st_gdp = [8.6, 8.5, 7.8, 7.1]
print(usa_st_gdp)
We will now use the + and [] operators to add the real GDP % of the new state Indiana
or the state that will become the fifth state in the list.
usa_st_gdp = usa_st_gdp + [6.9]
View the list using the below code.
print(usa_st_gdp)
This is how to insert an item at end of a list in Python, use + and [] Operator.
Read Python Program for even or odd
Method 4: To insert an item at end of a list in Python, use slicing method
Create a list of items that is the real GDP percentage of the top 4 states in the USA such as Tennessee = 8.6
, New Hemsphire = 8.5
, California = 7.8
and Nevada = 7.1
using the below code.
usa_st_gdp = [8.6, 8.5, 7.8, 7.1]
print(usa_st_gdp)
Now use the slicing method to add the real GDP % of the new state Indiana
or the state that will become the fifth state in the list.
usa_st_gdp[4:5] = [6.9]
Show the list using the below code.
print(usa_st_gdp)
We have learned how to insert the item at the end of the list in Python in different ways such as using slicing, insert, (+ and []) operators and append() methods.
You may like the following Python tutorials:
- Python Program to Check Leap Year
- Concatenate multiple lists in Python
- Python program to find the area of square
- Python program to find an area of a rectangle
- Python program to print pattern
- How to get the index of an element in Python List
- Python program to reverse a string with examples
- Python List pop() method [With Examples]
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.