Python list append() method appends an element at the end of the list. It returns None because it does not return any value and modifies the original list.
In the above figure, we are adding an element 10 at the end of the list.
main_list = [19, 18, 21, 29] print(len(main_list)) # Output: 4 element = 10 main_list.append(element) print(main_list) # Output: [19, 18, 21, 29, 10] print(len(main_list)) # Output: 5
The append() method can also accept a collection, which can be a list, dictionary, or any collection object, and adds it as a single element.
Syntax
list.append(element)
Parameters
Argument | Description |
element | It represents an item to add to the list. It can be any valid Python object. |
Adding a list as a single element
What if we have a list and need to append it to an existing list? It will be added as a single element. So, it will become a nested list.
main_list = [19, 18, 21, 29] print(len(main_list)) # Output: 4 other_list = [10, 20] main_list.append(other_list) print(main_list) # Output: [19, 18, 21, 29, [10, 20]] print(len(main_list)) # Output: 5
You can see that before the output, we have a normal list, but after the output, we have a nested list.
Invalid number of arguments
The append() method accepts only one argument at a time, and if you pass multiple arguments in the form of multiple elements, it will throw this error: TypeError: list.append() takes exactly one argument (2 given)
main_list = [19, 18, 21, 29] print(len(main_list)) # Output: 4 element_a = 10 element_b = 20 main_list.append(element_a, element_b) # Output: TypeError: list.append() takes exactly one argument (2 given)
To prevent this type of error, always pass a single argument.
Appending multiple elements
To append multiple elements in a Python list, you can use a for loop and, in each iteration, use the append() method.
original_list = [1, 2, 3] new_items = [4, 5, 6] for item in new_items: original_list.append(item) print(original_list) # Output: [1, 2, 3, 4, 5, 6]
Avoid appending in loops for large datasets if order doesn’t matter.
Appending different data types
Let’s add different types of elements to a list one at a time.
original_list = [21] original_list.append(19) # Integer original_list.append("hello") # String original_list.append([1, 2, 3]) # List original_list.append({"key": "value"}) # Dictionary original_list.append(None) # NoneType print(original_list) # Output: [21, 19, 'hello', [1, 2, 3], {'key': 'value'}, None]
Appending to an empty list
If the input list is empty, and you add an element, it will become the first element at index 0.
empty_list = [] empty_list.append(11) print(empty_list) # Output: [11]
Comparison with the list.insert()
The list.insert() method adds an element at a specific position, while the list.append() method adds at the end of the list.
# append() always adds to the end list1 = [1, 2, 3] list1.append(4) print(list1) # Output: [1, 2, 3, 4] # insert() adds at specific position list2 = [1, 2, 3] list2.insert(1, 'inserted') print(list2) # Output: [1, 'inserted', 2, 3] # insert() at end (equivalent to append) list3 = [1, 2, 3] list3.insert(len(list3), 4) print(list3) # Output: [1, 2, 3, 4]
That’s all!