Append Method in Python

The append method in Python is a built-in list method used to add a single element to the end of a list. This operation is quite common and is specifically optimized in Python through the append method. Below is an example demonstrating how to use the append method:

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

# [Output: [1, 2, 3, 'four']](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-5/49)

In this example, the string "four" is appended to the list x, resulting in the list [1, 2, 3, 'four'].

Appending a List to Another List

A common issue arises when attempting to append one list to another using the append method. Instead of merging the two lists, the entire second list is added as a single element to the first list. Consider the following example:

x = [1, 2, 3, 4]
y = [5, 6, 7]
x.append(y)  
x

# [Output: [1, 2, 3, 4, [5, 6, 7]]](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-5/49)

In this case, the list y is appended to the list x as a single element, resulting in the list [1, 2, 3, 4, [5, 6, 7]]. This behavior is important to understand when working with lists in Python, as it may not be the intended outcome if you wish to combine the elements of two lists.

To merge two lists, the extend method should be used instead of append. The extend method allows you to add each element of one list to another, effectively combining them into a single list.