"
This article is part of in the series

Python List Append codes

The Stack Overflow Developer Survey 2022 confirmed that roughly six out of ten people learning to code begin their programming journey with Python. 

And it's no wonder – Python is concise, easy to read, and object-oriented. It offers everything you'd expect a modern programming language to bring to the table.

Adding items to lists is a common problem programmers must solve. Python provides the .append() function built-in to help you solve it. 

.append() is used to add items to the end of existing lists. Interestingly, we can also use the function in for loop to populate lists programmatically. 

In this quick guide, we'll walk you through using .append(). 

What is .append()? What Can it Do?  

.append() accepts individual items as parameters and puts them at the end of the given list. 

It doesn't return the new list of items, like the filter() function would in Python. In fact, it doesn't return any value. The inputted list is modified by adding the expected items to its end.

When you use .append() on a list, its size increases by one. The nice thing about the function is that it can add all data types to the list. 

Besides strings and numbers, it can also insert dictionaries and lists at the end of a list. If you pass a list to this function, it will work with it as if it were a single object. 

Syntax of .append()

list.append(item)

The only parameter the function accepts is the item you want it to add to the end of the list. As mentioned earlier, no value is returned when you run this function. 

Adding Items to Lists with .append()

Accepting an object as an argument, the .append function adds it to the end of a list. Here's how:

>>> numberList = [57, 79, 43]
>>> numberList.append(6)
>>> numberList
[57, 79, 43, 6]

As you can see, when you call .append() on a list that already exists, it adds the supplied object to the right side of the list. The nice thing about lists in Python is that the language reserves memory for new items to be added later. So, all .append() does is place those items in the reserved memory. 

.append() can work with any type of object and add it to the end of a list, like so: 

>>> mixedList = [39, 29]

>>> mixedList.append(65)
>>> mixedList
[39, 29, 65]

>>> mixedList.append("string")
>>> mixed
[39, 29, 65, 'string']

>>> mixedList.append(9.0)
>>> mixed
[39, 29, 65, 'string', 9.0]

Adding various types of objects is possible because lists are flexible with data types. In the example above, we've added an integer, string, and floating point number to a list. But as mentioned earlier, .append() can also add a tuple, a dictionary, and even a user-defined object to lists. 

The .append() function is basically a sophisticated alternative for the following operation: 

>>> numberList = [57, 79, 43]

>>> # Here's what running numberList.append(31) basically does:
>>> numberList[len(numberList):] = [31]
>>> numberList
[57, 79, 43, 31] 

In the lines above, we're doing two things. First, we're taking a slice from numberList, and second, we're assigning an iterable to it. 

The slicing operation takes up the space after the final item in numberList. Simultaneously, the assignment operation unpacks the list's items to the operator's right, which are then added to numberList.

Though it might not be immediately apparent, there's a difference between adding objects to a list this way and using .append(). Using the assignment operator, you can add several objects to the list in one go:

>>> numberList = [57, 79, 43]

>>> numberList[len(numberList):] = [39, 29]
>>> numberList
[57, 79, 43, 39, 29]

In the code above, a slice is taken from the end of numberList, unpacked on the right, and added to the slice as individual items.

There's nothing inherently wrong with using .append() for the same purpose. The advantage of using .append() is that it can work with any data type and object. However, it can only add a single object to a list at a time:

>>> listOne = [11, 63, 89, 44]
>>> tupleOne = (77, 21)

>>> listOne.append(tupleOne)
>>> listOne
[11, 63, 89, 44, (77, 21)]

Above, the .append() function puts the tuple at the end of the supplied list, listOne. But what if you need to add individual items in the tuple at the end of listOne? 

The .append() function won't be able to help you there. But you can use the .extend() function. 

>>> listOne = [11, 63, 89, 44]
>>> tupleOne = (77, 21)
>>> listOne.extend(tupleOne)
>>> listOne
[11, 63, 89, 44, 77, 21]

>>> listOne = [11, 63, 89, 44] 
>>> tupleOne = (77, 21)
>>> # Equivalent to listOne.extend(tupleOne)
>>> listOne[len(listOne):] = tupleOne
>>> listOne
[11, 63, 89, 44, 77, 21]

You might be able to make out that .extend() accepts the iterable as an argument, unpacks it, and adds the items to the end of the target list. As you can see in the second half of the code, .extend does the same thing that listOne[len(listOne):] = tupleOne does.

Another characteristic of the .append() function is that it returns None – it doesn't return a new list with an additional item. This means that it does its work in place by modifying and growing the underlying list. 

>>> listOne = [56, 96]
>>> listTwo = listOne.append(116)
>>> listTwo is None
True
>>> listOne
[56, 96, 116]

As you can see, listTwo remained untouched even after using an assignment operation. Like many similar Python functions, .append() changes the underlying list and doesn't use another list to complete its task. 

Many new programmers forget this aspect of .append() and try to use the return value of .append() only to get an unexpected result. Take note of this characteristic, and you'll avoid it affecting your code. 

Populating a List with .append()

Python programmers often use the .append() function to add all the items they want to put inside a list. This is done in conjunction with a for loop, inside which the data is manipulated and the .append() function used to add objects to a list successively. 

Let's assume that you want to write a function that accepts a number sequence and returns a list with the square roots of the numbers. Here's what this program would look like:

>>> import math

>>> def square_root(numberList):
...     result = []
...     for number in numberList:
...         result.append(math.sqrt(number))
...     return result
...

>>> numberList = [16, 25, 36]
>>> square_root(numberList)
[4.0, 5.0, 6.0]

In this code, the square_root() function accepts numberList. Then, inside the function, we create an empty list "result" and use a for loop to iterate over the items in numberList. 

The math.sqrt() function is used in every iteration, working out the square root of the number, before .append() puts the result in the empty list "result." The loop ends, and the final list is returned. 

Though using for loops in tangent with .append() is common practice, other convenient constructs in the language make this process more Pythonic and efficient. 

Conclusion

Besides working with lists, the .append() function can also create stacks and queues. It also works with many other data structures, including but not limited to collection.deque() and array.array(). 

We hope this guide served as a good starting point to learn about .append()!