Python how to make an empty list

Learn how to create an empty list in Python with an example: Create my_list = [] and assign it to a variable.

Creating an Empty List in Python

An empty list is a Python data structure used to store a collection of items. This can be done in a few ways. The simplest way is to use square brackets to create an empty list:


empty_list = []

This creates an empty list called empty_list. To verify that the list is indeed empty, we can check its length:


len(empty_list)
# Output: 0

The len() function returns the number of items in the list, in this case 0. We can also use the bool() function to check if the list is empty:


bool(empty_list)
# Output: False

The bool() function returns True if the list is not empty, or False if it is. In this case, it returns False because the list is indeed empty.

Another way to create an empty list is to use the list() function:


empty_list = list()

This works the same as the first method, but it is a bit more explicit. It is also possible to create an empty list with a specific size, filled with None values:


empty_list = [None] * 10

This creates a list with 10 None values. We can verify this by printing the list:


print(empty_list)
# Output: [None, None, None, None, None, None, None, None, None, None]

Finally, we can use the range() function to create an empty list with a specific size:


empty_list = [None] * 10
empty_list = list(range(10))

This creates a list with 10 elements, each element being None. We can again verify this by printing the list:


print(empty_list)
# Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Answers (0)