Create an empty list
What You'll Learn: In this tutorial, you'll discover how to create an empty list in Python. A list is like a container that can store multiple items of different types.
What is an Empty List?
An empty list is simply a list with no elements. You can think of it as an empty basket ready to be filled with items later.
Creating an Empty List
There are two common ways to create an empty list in Python:
Using Square Brackets
- You can create an empty list by assigning a variable with empty square brackets
[].
Example Code:
pythonmyList = [] print('The list:', myList) print('List length:', len(myList))Output:
The list: [] List length: 0This code creates an empty list and checks its length, which is zero since it's empty.
- You can create an empty list by assigning a variable with empty square brackets
Using the
list()Function- You can also use the
list()built-in function to create an empty list.
Example Code:
pythonmyList = list() print('The list:', myList) print('List length:', len(myList))Output:
The list: [] List length: 0This method also creates an empty list and confirms its length is zero.
- You can also use the
Summary:
In this Python tutorial, we learned how to create an empty list using two methods: square brackets [] and the list() function. An empty list is useful when you want to start with an empty container and add items to it later. Keep experimenting and have fun with lists in Python!