You can use Python's append list method to create an entirely new set of data and then drop it in an empty list. You can even use it to add more data to the end of an existing Python list if you want.

So what are some ways you can use the append method practically in Python? Let's find out in this article.

How to Append More Values to a List in Python

The .append() method adds a single item to the end of an existing list and typically looks like this:

        FirstList = [1, 2, 'MUO']
Item = "Orange"
FirstList.append(Item)
print(FirstList)

Output: [1, 2, 'MUO', 'Orange']

However, unlike Python's extend method, even if you're inserting a list, tuple, dictionary, or a set containing many items, the append method only adds it as a single item, resulting in a nested list. In essence, it doesn't remove them from their literals but adds them directly.

Take a look at the example below. In this case, let's put "Orange" in a list:

        Item = ["Orange"]
FirstList.append(Item)
print(FirstList)

Output: [1, 2, 'MUO', ['Orange']]

Let's work with a Python list containing more than one item:

        FirstList = [1, 2, 5, 8]
Item = ["Orange", "Shoes", 5, 6]
FirstList.append(Item)
print(FirstList)

Output: [1, 2, 5, 8, ['Orange', 'Shoes', 5, 6]]

Like the previous one, the code above outputs a nested list.

You can also append a nested list to an existing list:

        FirstList = [1, (3, 7)]
Item = [5, {"2", 5}, {"Name":"Idowu"}, "Last item"]
FirstList.append(Item)
print(FirstList)

Output: [1, (3, 7), [5, {'2', 5}, {'Name': 'Idowu'}, 'Last item']]

You can append new items to an empty list:

        Empty_list = []
New_list = [1, 4, 8, 6]
Empty_list.append(New_list)
print(Empty_list)

Output: [[1, 4, 8, 6]]

Using Python's Append With the for Loop

Like we stated earlier, the .append() method adds a single item to the end of a list, which means if you're appending a Python list or any other data type to an existing list, you end up getting a nested list.

However, you can still force the .append() method to add individual items directly without creating a nested list by using the for loop; this is a bit similar to using the .extend() method:

        Empty_list = []
New_list = [1, 4, 8, 6]
for items in New_list:
  Empty_list.append(items)
print(Empty_list)

Output: [1, 4, 8, 6]

To see the similarities between them, let's replace .append() in the code above with .extend():

        Empty_list = []
New_list = [1, 4, 8, 6]
Empty_list.extend(New_list)
print(Empty_list)

Output: [1, 4, 8, 6]

Using a for loop in the above example doesn't work, as .extend() isn't iterable.

Related: How to Use For Loops in Python

So what's the essence of using the for loop to append a list in that case when you can simply extend it? Here's the thing; there are many instances where you can't use the .extend() method this way.

To justify that, let's see how you can use .append() to insert the result of a mathematical operation in an existing list. For example, the code below inserts all even numbers between six and 19 into an existing list:

        My_list = [2, 4]
List_to_append = range(6, 20)
for new in List_to_append:
if new % 2 == 0:
My_list.append(new)
print(My_list)

Output: [2, 4, 6, 8, 10, 12, 14, 16, 18]

Although the .append() method works the same way .extend() does when you use it with the for loop, you can't use .extend() to solve the last problem above due to the following reasons:

  • You're using the if statement to check for items that satisfy a particular condition in a list.
  • You need to loop through the list so that Python can check for the items you're looking for.
  • The .extend() method isn't iterable, so you can't use a loop with it.
  • If you choose not to loop and use the .extend() directly the way we did previously, there's no way Python can check each item in the list. Hence, resulting in an error.

You can also append the result of a mathematical operation into an empty list. Take a look at a further example below for appending all multiples of three between one and 12:

        Empty_list = []
List_to_append = range(1, 5)
for new in List_to_append:
new = new * 3
Empty_list.append(new)
print(Empty_list)

Output: [3, 6, 9, 12]

The append method is also useful in functions. Let's modify the code for inserting all even numbers into a function:

        def mat(data):
lits = [1, 2]
for datas in data:
if datas % 2 == 0:
lits.append(datas)
return lits
print(mat(range(1, 20)))

Output: [1, 2, 2, 4, 6, 8, 10, 12, 14, 16, 18]

The Append Method Is More Useful Than You Think

Now that you've seen some examples of how to append a list in Python, you might still be a bit skeptical about how it helps you in real-life projects. However, many background operations surround most of the Python modules and frameworks we use today.

For example, the append method can come in handy when you want to write an algorithm for a data science module or any framework. Depending on how you intend to achieve your aim, it can also be helpful during activities like unit testing, among others. So feel free to get acquainted with it like any other Python method.