Append to Empty List in Python (3 Examples)

 

In this article, I’ll show how to add objects to an empty list in the Python programming language.

Table of contents:

Let’s dive right into the examples!

Sample List

For the demonstration, first, we need to create an empty list.

my_list=[]                       # create empty list
print(my_list)                   # print my_list
# []

As shown above, now we have an empty list called my_list. The next step is to add some elements to it. Let’s do it!

 

Example 1: Add Items to Empty List via append()

In this section, I’ll explain how to append elements to my_list using the append() method. As an example, I will add the integer 1.

my_list.append(1)                # append 1
print(my_list)                   # print my_list
# [1]

As seen, now, my_list contains the element 1. What if we want to add multiple elements to my_list? In such a case, we need to use a for loop to repeat the appending operation for each element to be added. For demonstration, I will add the integers 1, 2, and 3.

for i in range(1, 4):            # append 1, 2 and 3
    my_list.append(i)
print(my_list)                   # print my_list
# [1, 2, 3]

The for loop has iterated through the sequence of numbers from 1 to 3 using the range() function.Then it appended these numbers via the append() method. As a result, my_list turned out to be a list consisting of the elements 1, 2, and 3.

Using for loops is just an option to add multiple items to a list. You can also opt for some Python methods specialized for inserting multiple items. Let’s take a look at them!

 

Example 2: Add Items to Empty List via extend()

The extend() method is a common list method for adding multiple elements to a list. For the implementation, please see below.

my_list.extend([1, 2, 3])        # append 1, 2 and 3
print(my_list)                   # print my_list
# [1, 2, 3]

As seen, just like using the append() method to add a single item to a list, you can use the extend() method with a list of items, like [1, 2, 3], to add multiple items to a list. You can also provide the numbers in a tuple (1, 2, 3) instead of a list.

Last but not least, another alternative is using the concatenation operator. Let’s see how it operates!

 

Example 3: Add Items to Empty List via Concatenation Operator

You can easily add elements to an empty list using the concatenation operator + together with the list containing the elements to be appended. See the formula below.

my_list + [1, 2, 3]              # append 1, 2 and 3
# [1, 2, 3]

As shown above, we have obtained a list with elements 1, 2, and 3 as a result. But please be aware that we haven’t printed my_list to obtain the final result like in the previous examples. Let’s see next what happens if we print my_list.

print(my_list)                   # print my_list 
# []

It looks like the elements have not been added to my_list. The reason is that the concatenation operator does not work in place, which implies that the original object, which is a list in this case, is not modified by the operation directly.

If you want to save the result in an object, you should explicitly assign an object name to it. I will use the name my_list since it is the object that I want to update. Please see the modified code below.

my_list = my_list + [1, 2, 3]    # append 1, 2 and 3
print(my_list)                   # print my_list
# [1, 2, 3]

Now, our list is updated by the given values. You can also prefer to save the result under a new object name and keep my_list empty.

 

Video & Further Resources

Do you need further information on the topics of this article? Then I recommend watching the following video on my YouTube channel. In the video, I demonstrate the Python programming codes of this post in Python.

 

The YouTube video will be added soon.

 

Furthermore, you may want to have a look at the other Python articles on my website. I have released several tutorials already.

 

Summary: In this Python article, you have learned how to append elements to empty lists. Let me know in the comments section if you have any additional questions.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top