Python: How to create an empty list and append items to it?

In this article, first we will discuss different ways to create an empty list and then we will see how to append elements to it using for loop or one liner list comprehension.

There are two ways to create an empty list in python i.e. using [] or list(). Let’s check out both of them one by one,

Create an empty list in python using []

In Python, an empty list can be created by just writing square brackets i.e. []. If no arguments are provided in the square brackets [], then it returns an empty list i.e.

# Creating empty List using []
sample_list = []

print('Sample List: ', sample_list)

Output:

Sample List:  []

Create an empty list in python using list() Constructor

Python’s list class provide a constructor,

list([iterable])

It accepts an optional argument i.e. an iterable sequence and it creates a list out of these elements. If Sequence is not provided then it returns an empty list. Let’s use this to create an empty list,

# Creating empty List using list constructor
sample_list = list()

print('Sample List: ', sample_list)

Output:

Sample List:  []

Difference between [] and list()

We can create an empty list in python either by using [] or list(), but the main difference between these two approaches is speed. [] is way faster than list() because,

  • list() requires a symbol lookup, as it might be possible that in our code, someone has assigned a new definition to list keyword.
  • Extra function call: As constructor will be called, so it is an extra function call.
  • Inside the constructor it checks if an iterable sequence is passed, if no then only it creates an empty list.

Whereas, [] is just a literal in python that always returns the same result i.e. an empty list. So, no additional name lookup or function call is required, which makes it much faster than list().

Create an empty list and append items

Till now we have seen two different ways to create an empty python list, now let’s discuss the different ways to append elements to the empty list.

Create an empty list and append elements using for loop

Suppose we want to create an empty list and then append 10 numbers (0 to 9 ) to it. Let’s see how to do that,

# Create an empty list
sample_list = []
# Iterate over sequence of numbers from 0 to 9
for i in range(10):
    # Append each number at the end of list
    sample_list.append(i)

Output:

Sample List:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

We used the range() function to generate an iterable sequence of numbers from 0 to 9. Then using the for loop, we iterated over that sequence and for each number in the sequence, we called the list’s append() function and passed the number to list.append() function, which adds the given item to the end of list in place.

Create an empty list and append items to it in one line using List  Comprehension

We will use the range() function like the previous example to generate an iterable sequence of numbers from 0 to 9. But instead of calling append() function, we will use List comprehension to iterate over the sequence and add each number at the end of the empty list. Let’s see how to do that,

# Append 10 numbers in an empty list from number 0 to 9
sample_list = [i for i in range(10)]

Output:

Sample List:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Here we created an empty list and added elements to it in a single line.

Create an empty list and insert elements at end using insert() function

Python provides a function insert() i.e.

list.insert(index, item)

It inserts the item at the given index in list in place.

Let’s use list.insert() to append elements at the end of an empty list,

# Create an empty list
sample_list = []
# Iterate over sequence of numbers from 0 to 9
for i in range(10):
    # Insert each number at the end of list
    sample_list.insert(len(sample_list), i)

print('Sample List: ', sample_list)

Output:

Sample List:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

We iterated over a sequence of numbers (0 to 9) provided my range() function, for each number we called the list.insert() function and passed the number to it along with index size-1 i.e. end of the list.

Create an empty list and insert elements at start

Sometimes our requirement is little different i.e. instead of end we want to add elements at the start of an empty list. Let’s see how to that using list.index() function,

# Create an empty list
sample_list = []
# Iterate over sequence of numbers from 0 to 9
for i in range(10):
    # Insert each number at the start of list
    sample_list.insert(0, i)

print('Sample List: ', sample_list)

Output:

Sample List:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Here, we iterate over a sequence of numbers (0 to 9) provided by range() function, for each number we called the list.insert() function and passed the number to it along with index 0 i.e. start of the list.

The complete example is as follows,

def main():

    print('*** Create an empty list using [] ***')

    # Creating empty List using []
    sample_list = []

    print('Sample List: ', sample_list)

    print('*** Create an empty list using list() ***')

    # Creating empty List using list constructor
    sample_list = list()

    print('Sample List: ', sample_list)

    print('***** Create an empty list and append elements at end *****')

    print('*** Create an empty list and append elements using for loop ***')

    # Create an empty list
    sample_list = []
    # Iterate over sequence of numbers from 0 to 9
    for i in range(10):
        # Append each number at the end of list
        sample_list.append(i)

    print('Sample List: ', sample_list)

    print('*** Create an empty list and append in one line ***')

    # Append 10 numbers in an empty list from number 0 to 9
    sample_list = [i for i in range(10)]

    print('Sample List: ', sample_list)

    print('*** Create an empty list and insert elements at end ***')

    # Create an empty list
    sample_list = []
    # Iterate over sequence of numbers from 0 to 9
    for i in range(10):
        # Insert each number at the end of list
        sample_list.insert(len(sample_list), i)

    print('Sample List: ', sample_list)

    print('*** Create an empty list and insert elements at start ***')

    # Create an empty list
    sample_list = []
    # Iterate over sequence of numbers from 0 to 9
    for i in range(10):
        # Insert each number at the start of list
        sample_list.insert(0, i)

    print('Sample List: ', sample_list)


if __name__ == '__main__':
    main()

Output:

*** Create an empty list using [] ***
Sample List:  []
*** Create an empty list using list() ***
Sample List:  []
***** Create an empty list and append elements at end *****
*** Create an empty list and append elements using for loop ***
Sample List:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*** Create an empty list and append in one line ***
Sample List:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*** Create an empty list and insert elements at end ***
Sample List:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*** Create an empty list and insert elements at start ***
Sample List:  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top