Python has a few methods to add items to the existing list. These methods are:
- append
- insert
- 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:
For example:
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:
The list after append: [7, 12, 17, 22, 27, 32]
You can see, 32 is added at the end 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:
The list after append: [‘Python’, ‘List’, ‘append method’]
Adding item by list insert method
Syntax of using insert method
The insert method takes two parameters as shown below:
- 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:
You can see, the new item is added at the 1 index of the specified list.
The extend method – adding multiple items/sequence
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:
The list after extend: [6, 12, 18, 24, 30, 36, 42]
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:
You saw the list itself is added as using the append method.