About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2025. All rights reserved.
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:
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.
To create an empty list, all you need to do is to set it equal to “[]”, like so:
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:
This will output:
A second way to create an empty list is to do:
This produces the expected output:
An empty list when used in a boolean context evaluates to False. A non-empty list evaluates to True.
If you try:
You will get:
If on the other hand, you tried:
You will get:
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:
The result is:
(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.)
Another useful function is to be able to check if a list is empty. We can use the len() function for it:
This produces:
as expected.
A second way to do it is to simply evaluate the list in the conditional context as we suggested earlier:
This gives:
If your list is not empty and you desire to empty it, we can use the clear() method:
This yields:
Of course since lists are mutable, you could also have done:
with the same results:
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.
mytoys = ['car',34,'trampoline','Scrabble']
mytoys = []
print(mytoys)
[]
mytoys = []
print(len(mytoys))
0
mytoys = list()
print(mytoys)
print(len(mytoys))
[]
0
mytoys = list()
if mytoys:
print("I have some toys!")
else:
print("I have no toys - I am so sad!")
I have no toys - I am so sad!
mytoys = ['car','truck']
if mytoys:
print("I have some toys!")
else:
print("I have no toys - I am so sad!")
I have some toys!
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!")
Square brackets are faster!
mytoys = ['car','boat']
if (len(mytoys) == 0):
print("List is empty")
else:
print("List is not empty")
List is not empty
mytoys = ['car','boat']
if (not(mytoys)):
print("List is empty")
else:
print("List is not empty")
List is not empty
mytoys = ['car','boat']
print(mytoys)
mytoys.clear()
print(mytoys)
['car', 'boat']
[]
mytoys = ['car','boat']
print(mytoys)
mytoys = []
print(mytoys)
['car', 'boat']
[]