How to declare an empty list in Python

Posted in Python by Dirk - last update: Feb 09, 2024

In Python, a list is a mutable, ordered collection of elements, which can be of different data types. Lists are defined by enclosing the elements in square brackets [] and separating them with commas. Lists support various operations, such as indexing, slicing, appending, and more

To create an empty list in Python, you can use either square brackets ([]): empty_list = [] or the list() constructor: empty_list = list().

Create a List using Square Brackets []

This is the most straightforward method. You simply create an empty list by enclosing square brackets with no elements inside.

empty_list = []

Using the list() Constructor

The list() constructor can be used to create an empty list. It can also be used to convert other iterable objects to lists.

empty_list = list()

The list() function in Python can take an optional iterable as its argument. If provided, it creates a new list containing the elements of that iterable. If no argument is provided (like in the example above), it returns an empty list.

Syntax:

list([iterable])

with

  • ├Čterable (optional): An iterable object (e.g., another list, tuple, string, etc.) whose elements will be used to create the new list.

Example

# Using list() with an iterable (creating a list from a string)
string_list = list("hello")
# Result: ['h', 'e', 'l', 'l', 'o']

# Using list() without an iterable (creating an empty list)
empty_list = list()
# Result: []

In the first example, the list() function is used to convert a string into a list of its individual characters. In the second example, list() is called without an argument, creating an empty list.

How can you check if a list is empty?

You can check if a list is empty in Python using various methods

Using the not Operator

The not operator is used to check if the list is empty. It evaluates to True if the list is empty and False if it has elements.

my_list = []

if not my_list:
    print("The list is empty.")
else:
    print("The list is not empty.")

Using the len() Function

The len() function returns the length of the list, and you can check if it is equal to 0 to determine if the list is empty.

my_list = []

if len(my_list) == 0:
    print("The list is empty.")
else:
    print("The list is not empty.")

Direct comparison

You can directly compare the list to an empty list ([]) to check for emptiness.

my_list = []

if my_list == []:
    print("The list is empty.")
else:
    print("The list is not empty.")

How to add values to a list

Using the append() Method

The append() method is used to add an element to the end of the list.

my_list = []
my_list.append(42)
my_list.append("hello")

print(my_list)

Output:

[42, 'hello']

Using the insert() Method

The insert() method is used to insert an element at a specific index in the list.

my_list = ['the','is','beautiful']
my_list.insert(1, "world")

print(my_list)

Output:

['the', 'world', 'is', 'beautiful']

In the example above, 'world' is inserted at position 1 of the list (the index of a list starts at 0)

Using the extend() Method or += Operator

The extend() method or the += operator is used to add multiple elements (from an iterable) to the end of the list.

my_list = []
my_list.extend([1, 2, 3])

print("List with first extension", my_list)

# or
my_list += [4, 5, 6]

print("List with second extension", my_list)

Output:

List with first extension [1, 2, 3]
List with second extension [1, 2, 3, 4, 5, 6]

Other articles