Insert Elements to Specified Location in List in Python

Last Updated on December 26, 2020 by Roshan Parihar

In this tutorial, learn how to append or insert elements to list in Python. The short answer is: use python insert() or append() to add elements to the list. You can add a single element and multiple elements with the examples given here.

Adding elements to the last location of the list using the Python functions. You can also add the list element to the desired location with the given examples.

So, let’s start learning each method to insert elements to list in Python.

Python Append() to Add Element to the End of List

You can add an element to the last location of the list using the Python append(). The function requires a single argument as the element of the list to insert.

After you execute the code in Python, the element gets added to the end of the list.

Output

[5, ‘Six’, 7, ‘Eight’, 9, ‘Ten’, ‘Three’]

The above example showing the element added to the end of the list. However, you can add an element to the desired location of the list. To learn how to add an element to the desired location, read further.

Insert Item to The Desired Location With Insert() in Python

If you want to insert the item to the desired location of the list. You have to use the Python insert(). The function requires two arguments.

The first argument is the index location where you want to add the element. Now, you have to pass the second argument which is the element you want to add.

After you execute the code, the element gets added to the desired location of the list.

Output

[5, ‘Six’, ‘Three’, 7, ‘Eight’, 9, ‘Ten’]

The above example showing the list element with the added element to the 2nd position. However, these methods add only a single element to the list.

If you want to add more elements in one go. You can add one element to the other with the example given below.

Add One List Elements to the Other Using Extend()

To add more than one element to the list, you have to use the extend() of Python. With this function, you can add all the items of one list element to the other element.

Output

[5, ‘Six’, 7, ‘Eight’, 9, ‘Ten’, 11, 12, ‘Thirteen’]

There are two list elements in the example given above. Using the extend(), the second list elements get added to the first list element. The example showing all the elements of the first list and the second list.

You may also like to read

I hope you like this post on how to append or insert elements to list in Python.

References