Hit enter after type your search item

How to add items to a list in Python

Python has a few methods to add items to the existing list. These methods are append, insert, and extend. Each of these methods is explained below with examples.

Append example Extend Example Insert Example

The Python append method of list

The append method adds an item at the end of a list. If you need to add an item to the specific position then use the insert method.

How to use the append method?

The syntax of using the append method for adding an item is:

list.append(object)

For example:

lst_ex.append(10)

You have to provide the object as a parameter in the append method. The append method updates the given list and does not return any value. See the following examples with code and output.

An example of adding to list by append method

In this example, a list of numeric objects is created. Initially, the list has five items. After that, an item is added to the list by using the append method.

The list is displayed before and after using the print function:

list append

See online demo and code


 

You can see, the 32 is added at the last of the list.

An example of adding a string object

In this example, a string object is appended by using the append method of the list. For that, a list of two items is created initially. This is followed by adding an item to the list by append method. Have a look:

Python append

See online demo and code

Adding item by list insert method

In certain scenarios, you need to add the items in existing list to the specific position rather than at the end of a list. For that, you may use the Python insert method of the list.

Syntax of using insert method

The insert method takes two parameters as shown below:

List.insert(i,x)

  • The i represents the index of the list that starts at 0.
  • The x is an object or item to be added to the specific position in the list.

An example of using the insert method

For the demo, a list of three items is created initially. After that, the insert method is used for adding an item at the 1 index of the list. See the code and output:

list insert

See online demo and code


You can see, the new item is added at the 1 index of the specified list.

The extend method – adding multiple items/sequence

The extend method of the list allows appending the items of a sequence to the given list. The sequence can be another list. So, if you want to add more than one item at one call, you may use the extend method.

See the example below for the usage of extend() method. Two lists are created which is followed by using the extend method. The list before and after the extend method is displayed.

list extend

See online demo and code


 

Difference between append and extend method

To see the difference between the extend and append methods, see the following example. In the example, four lists are created. Then, I used the append and extend methods to add one list to the other.

The print function displays the list after using the extend and append methods, so you may see the difference:

extend append

See online demo and code


 

You saw the list itself is added as using the append method.

This div height required for enabling the sticky sidebar