The Wayback Machine - https://web.archive.org/web/20250307202332/https://www.geeksforgeeks.org/python-list-append-method/
Open In App

Python List append() Method

Last Updated : 04 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

Python
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]

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.

Python List append() Method

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)

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 the extend() 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 the insert() 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 the extend() method instead.


Next Article

Similar Reads

three90RightbarBannerImg