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.

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 a 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:

#An example of append method

lst_append = [7,12,17,22,27]





print ("The list before append:",lst_append)



lst_append.append(32)



print ("The list after append:",lst_append)

Output:

list append

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:

#An example of append method with list of strings

lst_append_string = ['Python','List']


print ("The list before append:",lst_append_string)

lst_append_string.append('append method')


print ("The list after append:",lst_append_string)

Output:

Python append

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:

#An example of insert method

lst_insert = ['Python','List','method']



print ("The list before insert:",lst_insert)

lst_insert.insert(1, 'insert')



print ("The list after insert:",lst_insert)

Result:

list insert

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.

 

#An example of extend method

lst_extend = [6,12,18,24]

lst_extend2 = [30,36,42]

print ("The list before extend:",lst_extend)


lst_extend.extend(lst_extend2)

print ("The list after extend:",lst_extend)

Output:

list extend

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:

#An example of extend vs append method

lst_extend = [6,12,18,24]

lst_extend2 = [30,36,42]

#Using extend method

lst_extend.extend(lst_extend2)

print ("The list after extend:",lst_extend)

#Using append method
lst_append1 = [6,12,18,24]

lst_append2 = [30,36,42]

lst_append1.append(lst_append2)

print ("The list after append:",lst_append1)

Output:

extend append

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

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!