Python List append() Method
The append() method in Python is used to add a single item to the end of list. This method modifies the original list and does not return a new list.
Let’s look at a simple append() method example.
a = [2, 5, 6, 7]
# Use append() to add the element 8
# to the end of the list
a.append(8)
print(a)
Output
[2, 5, 6, 7, 8]
Table of Content
Syntax of append() method
list.append(element)
Parameter
- element: The item to be appended to the list. This can be of any data type(integer, string, list, etc.) ,the parameter is mandatory and omitting it can cause an error.
Return Type:
- The
append()method does not return any value, it just modifies the original list in place.
-Method.webp)
Examples of append() Method
Here are some examples and use-cases of list append() function in Python.
1. Appending Elements of Different Types
The append() method allows adding elements of different data types (integers, strings, lists, or objects) to a list. Python lists are heterogeneous meaning they can hold a mix of data types.
a = [1, "hello", 3.14]
a.append(True)
print(a)
Output
[1, 'hello', 3.14, True]
- Explanation: In this list a contains elements of different data types (integer, string, float), and append(True) adds a boolean True to the end of the list.
2. Appending List to a List
When appending one list to another, the entire list is added as a single element, creating a nested list.
a = [1, 2, 3]
a.append([4, 5])
print(a)
Output
[1, 2, 3, [4, 5]]
- Explanation: The append() method adds the list [4, 5] as a single element to the end of the list a, resulting in a nested list.
Frequently Asked Questions on append() Method
Can append() add multiple elements at once?
No,
append()can only add one element at a time. To add multiple elements we use theextend()method or we can use the append() method in a loop.
What is the time complexity of the append() method?
The
append()method has a time complexity of O(1) (constant time) because it adds an element to the end of the list without requiring any resizing or reordering.
Can the append() method add an element at a specific index in the list?
No, the
append()method only adds elements to the end of the list. If we need to insert an element at a specific index we can use theinsert()method.
Can the append() method add another list to an existing list without creating a nested list?
No, the
append()method will always add the entire list as a single element resulting in a nested list. To merge the contents of another list without nesting we use theextend()method instead.


