Python is a versatile programming language widely used in machine learning and artificial intelligence. It has a large inbuilt library. In this library, an append() function adds a single element to the end of a list. It is a fundamental Python programming operation widely used in various applications. This article explains everything about the append() function, which is built into Python.
What is the append() function in Python?
The ‘append()’ function in Python is a built-in method for adding a single element to the end of a list. The append() function modifies the list by placing the specified element at its last position. This method is very straightforward, requiring only the element to be added as its parameter. It is commonly employed to dynamically expand lists during program execution. This function does a new list but modifies the original list instead.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Syntax of append() Function in Python
The append function in Python has the following syntax:
list.append(element)
Here is the explanation:
● list: The list is used to store the elements in the array.
● append(): This method adds an element to the end of the list.
● Element: This is the item you want to append to the list. It can be any data type, such as integers, strings, or other lists.
Parameters of append() function in Python
The append() function only accepts one parameter. This parameter is mandatory. The developer cannot omit it. It can be an integer, floating number, string, list user-defined object, etc.
Return Value of append() function in Python
The append function does not return anything. It only modifies the original list.

82.9%
of professionals don't believe their degree can help them get ahead at work.
Examples of append() function in Python
In this example, we will add elements like integer floating numbers and a string to a list. The following program demonstrates the example append() function:
Program
list1 = ['Water', 5, 'Solid']
list1.append(10)
list1.append('python')
list1.append(27.5)
# Updated list after adding new elements
print(list1)
Output
['Water', 5, 'Solid', 10, 'python', 27.5]
Example 2: Adding iterable to the existing list
Here, we will add iterable like lists, sets or a dictionary to an existing list.
Program
list1 = ['programming', 5, 'Hello Harry Potter']
list1.append(['python', 'javascript', 50])
list1.append({50,40,10,10,20})
list1.append({"key1":"Modify", "key2":"Computer"})
print(list1)
Output
['programming', 5, 'Hello Harry Potter', ['python', 'javascript', 50], {40, 50, 10, 20}, {'key1': 'Modify', 'key2': 'Computer'}]
Example 3: Nested Lists
The following program demonstrates the Nested list. This program will append list2 to list1 and another list3 to list3. Let’s see what happens.
Program
list1 = ['50','51','52']
list2 = ['53','54','56','57']
list1.append(list2)
# Displaying the original list
print(list1)
# Nested list
list2.append(['101','102','103'])
# Displaying the original list
print(list1)
Output
['50', '51', '52', ['53', '54', '56', '57']]
['50', '51', '52', ['53', '54', '56', '57', ['101', '102', '103']]]
Using Append with collections.deque()
Using ‘append’ with ‘collection.deque’ is a straightforward and efficient way to add elements to the end of a deque (double-ended queue) in Python. The Collection.deque class is part of Python’s collections module and provides an easy-to-use data structure for fast appends and pops from both ends.
The following program demonstrates the append function with collection.deque().
Program
from collections import deque
d = deque(['a', 'b', 'c'])
d.append('d')
d.append('e')
for char in ['f', 'g', 'h']:
d.append(char)
d.appendleft('z')
print(d)
Output
deque(['z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
Using Append with array.array()
In Python, ‘array.array’ is a module that provides an array object that can store elements of a specific type. Unlike python lists, which are more flexible but can store elements of mixed types, ‘array.array’ requires you to specify a type code that indicates the types of elements that it will store. The following program demonstrates the append method with array.array().
Program
import array
# Create an array of floating-point numbers
float_array = array.array('f', [1.1, 2.2, 3.3])
# Append a new element
float_array.append(4.4)
# Print the updated array
print(float_array)
float_array2 = array.array('f',[1.2, 2.2, 3.4, 5.9, 9.9])
print(float_array2)
Output
array('f', [1.100000023841858, 2.200000047683716, 3.299999952316284, 4.400000095367432])
array('f', [1.2000000476837158, 2.200000047683716, 3.4000000953674316, 5.900000095367432, 9.899999618530273])
Time Complexity for Append in Python
The append function has constant time complexity O(1). It means it operates in constant time. When you use the ‘append()’ method, Python typically adds the new element without needing to reallocate or move the existing elements, resulting in an average-case time complexity of O(1). However, in rare cases when the allocated space is exhausted, Python will need to reallocate and copy the list to a larger space, which can temporarily increase the time complexity to O(n). Despite this, the amortized time complexity of ‘append()’ remains O(1), making it a highly efficient operation for adding elements to a list.
Conclusion
The append() function is an essential method for adding elements to the end of a list in Python. Its simplicity and efficiency make it a go-to tool for dynamic list operations, allowing programmers to manipulate data structures seamlessly. Understanding the append() function is important for anyone working with lists in Python. By using, append() function, you can streamline your code, making it more efficient and readable. Whether you’re dealing with simple data aggregation or complex data structures, mastering the ‘append()’ function is a crucial step in becoming proficient in Python programming language.
What does the append()’ function do in Python?
What is the syntax for the ‘append()’ function?
Does ‘append()’ modify the original list?
How does ‘append()’ differ from ‘extend()’ ?
list1 = [1, 2, 3] list1.extend([4, 5]) print(list1) # Output [1, 2, 3, 4, 5]
What are some common use cases for the append() function?
Updated on July 25, 2024
