Skip to content

Python Tutorial: What Does append Do? Explained

[

Python’s .append(): Add Items to Your Lists in Place

Adding items to a list is a common task in Python, and one of the methods that can help with this is .append(). With .append(), you can add items to the end of an existing list object. Additionally, you can use .append() in a for loop to populate lists programmatically.

Table of Contents

  • Adding Items to a List With Python’s .append()
    • .append() Adds a Single Item
    • .append() Returns None
  • Populating a List From Scratch
    • Using .append()
    • Using a List Comprehension
    • Switching Back to .append()
  • Creating Stacks and Queues With Python’s .append()
    • Implementing a Stack
    • Implementing a Queue
  • Using .append() in Other Data Structures
    • array.append()
    • deque.append() and deque.appendleft()
  • Conclusion

Adding Items to a List With Python’s .append()

Python’s .append() is a method that takes an object as an argument and adds it to the end of an existing list. For example:

numbers = [1, 2, 3]
numbers.append(4)

After running this code, numbers will be [1, 2, 3, 4]. Every time you call .append() on an existing list, it adds a new item to the end of the list.

The .append() method can be used to add any type of object to a list. For example:

mixed = [1, 2]
mixed.append(3)
mixed.append("four")
mixed.append(5.0)

After running this code, mixed will be [1, 2, 3, 'four', 5.0]. Lists in Python can hold different data types and objects, so you can use .append() to add integers, strings, floating-point numbers, dictionaries, tuples, and more.

Populating a List From Scratch

In addition to adding items to an existing list, you can also use .append() to populate lists from scratch. There are two common ways to do this: using .append() or using a list comprehension.

Using .append()

You can use the .append() method in a for loop to add multiple items to a list one by one. For example:

numbers = []
for i in range(1, 6):
numbers.append(i)

After running this code, numbers will be [1, 2, 3, 4, 5]. The .append() method is called in each iteration of the for loop to add the current value of i to the numbers list.

Using a List Comprehension

Alternatively, you can use a list comprehension to create a list and populate it at the same time. Here’s an example:

numbers = [i for i in range(1, 6)]

After running this code, numbers will be [1, 2, 3, 4, 5]. The list comprehension [i for i in range(1, 6)] creates a new list by iterating over the range of numbers from 1 to 6 and adding each number to the list.

Using a list comprehension can be more concise and efficient compared to using a for loop with .append(), especially when dealing with large datasets.

Creating Stacks and Queues With Python’s .append()

In addition to building regular lists, you can also use .append() to create and manipulate stacks and queues.

Implementing a Stack

A stack is a type of data structure where items are added and removed from the top. You can implement a stack using the .append() method.

stack = []
# Add items to the stack
stack.append("item1")
stack.append("item2")
stack.append("item3")
# Remove items from the stack
stack.pop()
stack.pop()

After running this code, the stack will be ["item1"]. In this example, items are added to the stack using .append() and removed using the .pop() method. The last item added to the stack is the first one to be removed, following the LIFO (Last In, First Out) principle.

Implementing a Queue

A queue is another type of data structure where items are added to the back and removed from the front. You can implement a queue using .append().

queue = []
# Add items to the queue
queue.append("item1")
queue.append("item2")
queue.append("item3")
# Remove items from the queue
queue.pop(0)
queue.pop(0)

After running this code, the queue will be ["item3"]. In this example, items are added to the queue using .append() and removed using the .pop(0) method. The first item added to the queue is the first one to be removed, following the FIFO (First In, First Out) principle.

Using .append() in Other Data Structures

Besides lists, you can also use .append() in other Python data structures like arrays and double-ended queues.

array.append()

The array module in Python provides an array data structure. To add items to an array, you can use the .append() method.

import array
numbers = array.array("i", [1, 2, 3])
numbers.append(4)

After running this code, numbers will be array('i', [1, 2, 3, 4]). The .append() method works in the same way as with regular lists.

deque.append() and deque.appendleft()

The collections module in Python provides a deque (double-ended queue) data structure. With the deque, you can add items to either the right or left end of a queue using the .append() and .appendleft() methods, respectively.

from collections import deque
queue = deque([1, 2, 3])
queue.append(4)
queue.appendleft(0)

After running this code, queue will be deque([0, 1, 2, 3, 4]). The .append() method adds an item to the right end of the queue, while .appendleft() adds an item to the left end.

Conclusion

Python’s .append() method is a useful tool for adding items to lists and other data structures. It helps with populating lists, creating stacks and queues, and working with arrays and double-ended queues. By understanding and utilizing .append() effectively, you can write more efficient and readable Python code.