Python list append() method adds an item to the end of the list. It appends an element by modifying the list. The method does not return itself. The item can also be a list or dictionary which makes a nested list.
The following is the syntax of the list sort() method:
Let's see some examples of the append() method to understand it's functionality.
First, let's see a simple example which appends elements to the list.
Output:
Given List: [12, 16, 20, 24] Updated List: [12, 16, 20, 24, 28]
Explanation:
Here, we are given a list consisting of some elements. We used the append() method to add a new element, 28, to the list. As a result, the element is added at the last of the list.
In Python, we can append a list to a list making it a nested list. Here is an example to understand how to do it using list append() method.
Output:
List 1: [11, 15, 19] List 2: [10, 20, 30] Nested List: [11, 15, 19, [10, 20, 30]]
Explanation:
In the above example, we are given two lists consisting of some elements. We used the append() method to add the second list to the first list. This creates a nested list.
Appending multiple lists to the single list will create a nested list. Here, two lists are appended to the same list and generates a list of the multiple lists. See the example below to understand about the nested list.
Output:
List 1: [11, 15, 19] List 2: [10, 20, 30] Nested List: [11, 15, 19, [10, 20, 30, [15, 30, 45]]]
Explanation:
In this example, we are given multiple lists. We used the append() method to append the second list to the first list. We then again used the append() method to add a new list to the second list. As a result, the first list is also updated showing the nested list.
We request you to subscribe our newsletter for upcoming updates.