Python List append() Method (original) (raw)
Last Updated : 18 Mar, 2025
**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 an example to better understand this.
Python `
a = [2, 5, 6, 7]
Use append() to add the element 8 to the end of the list
a.append(8) print(a)
`
**Explanation: append(8) adds 8 to the end of the list a, modifying it in place.
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, object, etc.). This parameter is mandatory, and omitting it will cause an error.
**Return:
- append() does not return any value. It modifies the original list in place.
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.
Python `
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.
Python `
a = [1, 2, 3]
a.append([4, 5]) print(a)
`
**Explanation: append() method adds the list [4, 5] as a single element to the end of the list a, resulting in a nested list.
3. Appending Using a Loop
Appending multiple elements using a loop:
Python `
a = [] for i in range(5): a.append(i) print(a)
`
Append vs Extend vs Insert
Method | Functionality |
---|---|
append(x) | Adds x as a single element at the end of the list. |
extend(iterable) | Adds all elements of iterable individually to the list. |
insert(index, x) | Inserts x at the specified index. |
Example demonstrating the difference between append and extend:
Python `
a = [1, 2, 3]
a.append([4, 5])
print(a)
b = [1, 2, 3]
b.extend([4, 5])
print(b)
`
Output
[1, 2, 3, [4, 5]] [1, 2, 3, 4, 5]
**Explanation:
- append([4,5]) adds [4,5] as a single element, creating a nested list ([1, 2, 3, [4, 5]]).
- extend([4,5]) adds each element separately, resulting in [1, 2, 3, 4, 5].