How to insert item at end of Python list [4 different ways]

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.

Methods to Insert Item At End of Python List

Here let us see 4 different ways to insert item at end of Python list:

  • insert() function
  • append() function
  • The + and [] Operator
  • slicing method

Let’s see them one by one using some illustrative examples:

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 Python list, the insert is made at the end of the Python list.

READ:  Python List count() method [With Examples]

Example: 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 Hampshire = 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 in Python.

usa_st_gdp.insert(len(usa_st_gdp), 6.9)

Check the list using the below code.

print(usa_st_gdp)
how to insert at the end of a list in python
To Insert Item At The End of The Python List

Look in the output, how we have inserted the item which is a float value at the end of the list in Python using the insert() function.

Method 2: Insert an item at end of a list in Python using the append() function

The most straightforward method to add an item to the end of a list in Python is using the append() method. This method takes a single argument, the item you wish to add, and appends it to the end of the Python list.

Example: 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 Hampshire = 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 in Python to add the real GDP % of the new state Indiana or the state that will become the fifth state on the list.

usa_st_gdp.append(6.9)

View the list using the below code.

print(usa_st_gdp)
how to insert at the end of a list in python
To Insert Item At The End of-The-Python List Using Append

This is how to insert an item at end of a list in Python using the append() function.

READ:  Find first number in string in Python

Method-3: Insert an item at end of a list in Python using + and [] Operator

The + operator in Python is used for concatenation when applied to lists. This means that it can be used to combine two Python lists together. When we want to append a single item to a list using the + operator, we need to place the item within its own Python list (or another iterable), ensuring that we are concatenating lists in Python.

Example: Create a list in Python of items that is the real GDP percentage of the top 4 states in the USA such as Tennessee = 8.6, New Hampshire = 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 on the Python list.

usa_st_gdp = usa_st_gdp + [6.9]

View the Python list using the below code.

print(usa_st_gdp)
python list insert at end
To Insert Item At The End of The Python List Example

This is how to insert an item at end of a list in Python, using + and [] Operator.

Note: Using the same variable name twice in a line of Python code is not considered good practice. So, in the above code, we can use the += operator instead of the + operator and can get the same output.

Method 4: To insert an item at end of a list in Python using the slicing method

Slicing is a versatile technique in Python used for accessing a specific range of items in a list. Though it is commonly used for retrieving parts of a list, we can also use slicing to modify lists, including appending items to them.

READ:  Python Pretty Print JSON

We will create a space at the end of the Python list using slicing and then assign that space to the item that we want to insert into the list.

Example: 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 Hampshire = 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 on the list in Python.

usa_st_gdp[4:5] = [6.9]

Show the list using the below code.

print(usa_st_gdp)
python list insert last
To Insert Item At The End of The Python List Using Slicing

Conclusion

We have learned how to insert the item at the end of the list in Python in different ways such as using the slicing method, insert() method, (+ and []) operators, and append() methods with some demonstrative examples.

You may like the following Python tutorials: