Kodeclik Blog
All about Python Empty Lists
Python lists are a very useful data structure. Remember that Python lists are an iterable, ordered, mutable, data structure. You can create a list of your toys like so:
mytoys = ['car',34,'trampoline','Scrabble']
Recall that lists need not contain elements of the same type. Furthermore, you an add to lists, delete elements from lists since they are mutable.
Creating an empty Python List using square brackets
To create an empty list, all you need to do is to set it equal to “[]”, like so:
mytoys = []
print(mytoys)
This produces:
[]
As shown above, the mytoys variable is initialized to an empty list containing zero elements. You can verify that it contains no elements by doing:
mytoys = []
print(len(mytoys))
This will output:
0
Creating an empty Python List using the list() constructor
A second way to create an empty list is to do:
mytoys = list()
print(mytoys)
print(len(mytoys))
This produces the expected output:
[]
0
Evaluating an empty list
An empty list when used in a boolean context evaluates to False. A non-empty list evaluates to True.
If you try:
mytoys = list()
if mytoys:
print("I have some toys!")
else:
print("I have no toys - I am so sad!")
You will get:
I have no toys - I am so sad!
If on the other hand, you tried:
mytoys = ['car','truck']
if mytoys:
print("I have some toys!")
else:
print("I have no toys - I am so sad!")
You will get:
I have some toys!
Which is more efficient? Square brackets or list constructor?
The square brackets approach is the more efficient way to create an empty list because the list constructor is more general purpose and uses more checks and logic before the list is created.
You need not take our word for it. Let us use the timeit() module to time two pieces of code. Below is a program that declares both pieces of code and times them:
import timeit
code1 = 'mytoys = []'
code2 = 'mytoys = list()'
time1 = timeit.timeit(code1)
time2 = timeit.timeit(code2)
if (time1 < time2):
print("Square brackets are faster!")
else:
print("List constructors are faster!")
The result is:
Square brackets are faster!
(Try running this program a few times to convince yourself that this is a consistent pattern. In fact the speed difference is at least 1-2 orders of magnitude.)
Checking if a list is empty
Another useful function is to be able to check if a list is empty. We can use the len() function for it:
mytoys = ['car','boat']
if (len(mytoys) == 0):
print("List is empty")
else:
print("List is not empty")
This produces:
List is not empty
as expected.
A second way to do it is to simply evaluate the list in the conditional context as we suggested earlier:
mytoys = ['car','boat']
if (not(mytoys)):
print("List is empty")
else:
print("List is not empty")
This gives:
List is not empty
Emptying a List
If your list is not empty and you desire to empty it, we can use the clear() method:
mytoys = ['car','boat']
print(mytoys)
mytoys.clear()
print(mytoys)
This yields:
['car', 'boat']
[]
Of course since lists are mutable, you could also have done:
mytoys = ['car','boat']
print(mytoys)
mytoys = []
print(mytoys)
with the same results:
['car', 'boat']
[]
In this blog post you have learnt all about empty lists: how to create them, how to test for emptiness, and how to empty a non-empty list.
Interested in more things Python? See our blogpost on Python's enumerate() capability, multiple ways to find the length of a Python list, and Python's ternary conditional operator. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.