The built-in Python append() function is used to add a single item to the end of an existing list. It modifies the original list by adding an item at the end, and it does not return any value (no new list).
As we all know, lists are mutable, so we can add or remove items after creation. The Python append() function accepts any data type and inserts that value to the end of the existing list.
Python append list syntax
The syntax of the append() function for adding an element/item to a list is as shown below.
oldList.append(new_item)
Here, oldList is an existing list that can be empty or have a few items. The Python append() function adds the given item (new_item) to the end of the oldList.
Parameter: As you can see from the above append() function syntax, it accepts one mandatory argument. It is the item that will be appended to the end of the existing list. This new_item can be of any supporting data type, including integer, float, string, Boolean, list, or any other object.
Return Value: The Python append() list function won’t return any value as output. It simply adds an item to the existing list. We must use the print() function to view the updated list with the new item at the end.
Python append list function Example
In the following example, we created an integer list with four integer values. The first print() statement returns the original list. Next, we used the list append() function to add a new item (50) to the existing “a” list.
As we mentioned earlier, the append() adds a new item (50) at the end of the existing list “a”. The final list is [10, 20, 30, 40, 50]. Please observe that 50 is in last place in the updated list.
TIP: Please refer to the List and List Functions articles in Python to learn about creating, accessing lists, and available methods.
a = [10, 20, 30, 40]
print("Original Items are : ", a)
a.append(50)
print("New Items are : ", a)
Result
Original Items are : [10, 20, 30, 40]
New Items are : [10, 20, 30, 40, 50]
Dynamically adding elements to a list
By now, we all know that the Python append() function accepts a single parameter. To add multiple values, we must use the append() function multiple times. For instance, in the following example, we are adding three integer values to an existing integer list.
a = [10, 20, 30, 40, 50]
a.append(60)
a.append(70)
a.append(80)
print(a)
If you observe the output, it shows all three items that we inserted into a list. The method follows the same order that we used in the program for the insertion. For example, here, it first appends 60, then 70, and then 80. If you change the order, it follows the same.
[10, 20, 30, 40, 50, 60, 70, 80]
How to append Strings to a list?
As we mentioned earlier, the Python append() list function works with any data type. So, adding a string is the same as adding any other item (integer).
In this example, we declared a list of four string elements. Next, we used the append() function to add another fruit to the fruits list.
Fruits = ['Apple', 'Orange', 'Kiwi', 'Grape']
Fruits.append('Banana')
print(Fruits)
As you can see from the output below, a new string element ‘Banana’ is added at the end of the fruits list.
['Apple', 'Orange', 'Kiwi', 'Grape', 'Banana']
Populate an empty list using Python append() and a for loop?
In all our previous examples, we used the Python append() function to add an item to the end of the existing list elements. However, there are many situations where we might have to populate or add elements to an empty list. In such a situation, use the for loop for iteration, and the append() function to insert one item at a time to populate the empty list.
In the following example, we declared an integer list and an empty list. Next, the for loop iterates over the elements in an existing list. Within the loop, the append() method calculates the square of each element and adds the result to the empty list.
n = [2, 4, 6, 8]
sq = []
for x in n:
sq.append(x ** 2)
print(sq)
[4, 16, 36, 64]
Populate an empty list using user input and Python append() function
In the previous example, we performed mathematical calculations on an existing list and added the result to an empty list. However, there are situations where we must allow the user to enter their own list items to perform various operations.
In these situations, we must declare an empty list and use the append() function to insert the user to enter the values into it.
In the example below, we allow the user to enter the length of a list (the maximum number of items a list can allow). Next, we used a for loop to iterate from 1 to that maximum number (4).
Within the loop, the input() statement asks the user to enter one list element at a time. Next, the Python append() function adds the user-entered value to that empty list. This process will continue until it reaches the maximum number (in this case, 4).
Apart from this, this program also allows the user to enter an extra item and use the append() function to add that number to the list built by the user’s inputs.
intLi = []
number = int(input("Enter the Total Number of Elements: "))
for i in range(1, number + 1):
value = int(input("Enter the %d Element : " %i))
intLi.append(value)
item = int(input("Enter the New Element : " ))
print("Original Elements are : ", intLi)
intLi.append(item)
print("New Elements are : ", intLi)

When using the append() function in a for loop, we must be careful with our approach. In the above example, on each iteration, we added a new value to the list. However, the example creates a list of lists because of [], the Python append() function creates a new list for each value.
n = []
for i in range(5):
n.append([i])
print(n)
[[0], [1], [2], [3], [4]]
To fix it, replace the n.append([i]) line with the n.append(i), and the result is [0, 1, 2, 3, 4].
Python append() function in Different data types
As we all know, the lists in Python are heterogeneous, meaning they can hold mixed data types. This section shows a series of examples by appending different data types to an existing list.
Adding a string to a number list
The following examples declared an integer list. Next, used the Python append() function to add a string to the existing number list. We can also append an integer to a string list (try yourself).
a = [1, 2, 3, 4]
a.append("Hi")
print(a)
[1, 2, 3, 4, 'Hi']
Adding a floating-point number to a string
In this example, we append a floating-point number to a string list. As the Python append() method inserts each passed item, there is no problem in appending a number to a string list.
a = ['USA', 'CHINA', 'INDIA']
a.append(43.58)
print(a)
['USA', 'CHINA', 'INDIA', 43.58]
Appending a Boolean value to a list
In the example below, we declared a list of mixed data types (string, int, and float). Next, we utilized the append() function to append a Boolean True value to this mixed list.
a = ['USA', 20.78, "CHINA", 30, "INDIA", 18]
a.append(True)
print(a)
As you can see, True is added to the end of the mixed list.
['USA', 20.78, 'CHINA', 30, 'INDIA', 18, True]
Python append() function to add a list to a list
Apart from the regular data types, the append() method also allows you to add a list to an existing list. Similar to the other data type, the appended list will be added at the end of the existing list.
However, instead of adding individual items, the append() function adds the complete list as a single item, which leads to a nested list.
a = [2, 4, 6, 8]
b = [10, 12]
a.append(b)
print(a)
If you observe the result below, the second list ([10, 12]) is added as a single item, and a normal list becomes a nested list.
[2, 4, 6, 8, [10, 12]]
TIP: Please use the list extend() method to add individual items in the second list to the first one.
The other option is to use the for loop to iterate over the second list’s elements and use the append() function. Here, the for loop iterates over the second list items (!0 and 12). Next, the Python append() function adds each item to the first integer list.
a = [2, 4, 6, 8]
b = [10, 12]
for n in b:
a.append(n)
print(a)
[2, 4, 6, 8, 10, 12]
Nested List
When working with multiple lists, we can use the append() function to add multiple lists as a nested list. The following example appends the b list and then appends the c list.
a = [2, 4, 6, 8]
b = [10, 12]
c = [1, 3]
a.append(b)
a.append(c)
print(a)
[2, 4, 6, 8, [10, 12], [1, 3]]
However, if we use [] inside the append() method, the result becomes multi level nested list.
a = [2, 4, 6, 8]
b = [10, 12]
c = [1, 3]
a.append([b, c])
print(a)
[2, 4, 6, 8, [[10, 12], [1, 3]]]
Python append() function to add a Tuple to a list
Similar to the lists, we can use the append() function to add a tuple to a list. Even for the tuple, the append() method treats the list of items in a tuple as a single item. When we use append() to add a tuple to a list, the tuple is nested inside a list.
a = [1, 3, 5, 7]
b = (8, 9, 10)
a.append(b)
print(a)
[1, 3, 5, 7, (8, 9, 10)]
Otherwise, use the for loop to iterate over the tuple elements, and on each iteration, use the append() method to add one item at a time.
for n in b:
a.append(n)
print(a)
[1, 3, 5, 7, 8, 9, 10]
Python append() function to add sets to a list
We can use the append() function to add a set to a list, and the append() treats a set as a single item and inserts it at the last position. When we use append() to add a set to a list, the set is nested inside a list.
a = [10, 20, 30]
b = {11, 22, 33}
a.append(b)
print(a)
10, 20, 30, {33, 11, 22}]
However, we can use list comprehension to iterate over the set elements and use append() to add a single set element at the last position.
a = [10, 20, 30]
b = {11, 22, 33}
[a.append(n) for n in b]
print(a)
[10, 20, 30, 33, 11, 22]
Python append() function to add a dictionary to a list
Apart from the above mentioned data structures, we can use the append() function to add a dictionary key-value pair to a list.
In the example below, we declared an empty list. Next, we used the append() function to add a dictionary key-value pair to a list. Here, we added three student records (Name, Age, and their Marks). To access each student record, we must use the list index position and the dictionary key. However, the for loop will print them as a table.
students = []
students.append({"Name": "John", "Age": 25, "Marks":98})
students.append({"Name": "Bruce", "Age":30, "Marks": 87})
students.append({"Name": "Tim", "Age": 27, "Marks": 99})
print(students)
for s in students:
print(s["Name"], s["Age"], s["Marks"])
Result
[{'Name': 'John', 'Age': 25, 'Marks': 98},
{'Name': 'Bruce', 'Age': 30, 'Marks': 87},
{'Name': 'Tim', 'Age': 27, 'Marks': 99}]
John 25 98
Bruce 30 87
Tim 27 99
Python append() function to filter data in a list
We can use the append() function to filter the data in an existing list and add it to a new list. For instance, filter the list comments by a specific keyword like “good”, “Bad”, etc.
In the following example, we declared a list of the first 10 natural numbers. Next, we used the for loop to iterate through those list items. On each iteration, the if statement checks whether the list element is perfectly divisible by 2. If true, the append() function adds that number to a new list. In short, the program below filters the even numbers in a given list and adds them to a new list.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = []
for i in a:
if i % 2 == 0:
b.append(i)
print(b)
[2, 4, 6, 8, 10]
Python append() vs extend() vs insert()
To insert items into a list, we can use the append(), extend(), and insert() methods. All three allow you to insert items into an existing list; they all differ in the insertion process.
- append(n): It inserts a single item at the end of the list. It is helpful to add a single item to the list’s end position.
- extend(iterable): It inserts all items in a given iterable into the list. Here, all items in a given iterable are individually inserted as normal list items. Use extend() to add multiple items to a list.
- insert(index, n): It inserts the given value (n) at the specified index. Use the insert() function to add items at specific index positions (not at the end).
To demonstrate it, we declared two list variables: the first list containing 3 integers and the second containing 2 integers.
a = [1, 2, 3]
b = [4, 5]
a.append(b)
print(a)
[1, 2, 3, [4, 5]]
If we use the extend(), the result is different. Replace the a.append(b) line with a.extend(b), and the result is
[1, 2, 3, 4, 5]
As you can see from the above, the Python append() function returns a nested list, whereas the extend() function returns a normal list.
The insert() function in the example below adds the second in the first index position. As the index starts from 0, [4, 5] is inserted in the second place.
a = [1, 2, 3]
b = [4, 5]
a.insert(1, b)
print(a)
[1, [4, 5], 2, 3]
Python append() function vs List comprehension
The append() function is a built-in list method to add a single item to the end of the list. To add multiple items, we must use the for loop or the list comprehension. However, if we use list comprehensions, we can skip the append() and the loop.
NOTE: This comparison is worth for appending multiple items to a list or adding items to an empty list.
In the example below, we declared an empty list and used the append function to fill it with square of each number (1, 2, 3, 4, 5). Here, the for loop iterates the numbers from 1 to 5 (6 not included). On each iteration, the i**2 code calculates the square of each number. The Python append() function adds that square value to the empty list.
n = []
for i in range(1, 6):
n.append(i**2)
print(n)
[1, 4, 9, 16, 25]
The above program can be written as the code below. It is simple and clean to read. It does not need the append function; the list comprehension will add them to a list.
n = [i ** 2 for i in range(1, 6)]
print(n)
[1, 4, 9, 16, 25]
TIP: The performance of list comprehension is faster compared to the append() method. Whereas, for the nested loops and complex calculations, append() will excel.