If you want to learn how to create an empty list in Python efficiently, then this article is for you.
You will learn:
- How to create an empty list using square brackets >>> num = [] >>> len(num) 08.
- How to create an empty list using >>> num = [] >>> len(num) 09.
- Their use cases.
- How efficient they are (one is faster than the other!). We will use the >>> num = [] >>> bool(num) False0 module to compare them.
Let's begin! ✨
🔹 Using Square Brackets
You can create an empty list with an empty pair of square brackets, like this:
💡 Tip: We assign the empty list to a variable to use it later in our program.
For example:
num = []The empty list will have length >>> num = [] >>> bool(num) False1, as you can see right here:
>>> num = [] >>> len(num) 0Empty lists are falsy values, which means that they evaluate to >>> num = [] >>> bool(num) False2 in a boolean context:
>>> num = [] >>> bool(num) FalseAdd Elements to an Empty List
You can add elements to an empty list using the methods >>> num = [] >>> bool(num) False3 and >>> num = [] >>> bool(num) False4:
- >>> num = [] >>> bool(num) False3 adds the element to the end of the list.
- >>> num = [] >>> bool(num) False4 adds the element at the particular index of the list that you choose.
Since lists can be either truthy or falsy values depending on whether they are empty or not when they are evaluated, you can use them in conditionals like this:
if num: print("This list is not empty") else: print("This list is empty")The output of this code is:
This list is emptyBecause the list was empty, so it evaluates to False.
In general:
- If the list is not empty, it evaluates to >>> num = [] >>> bool(num) False7, so the if clause is executed.
- If the list is empty, it evaluates to >>> num = [] >>> bool(num) False2, so the else clause is executed.
Example:
In the example below, we create an empty list and assign it to the variable >>> num = [] >>> bool(num) False9. Then, using a for loop, we add a sequence of elements (integers) to the list that was initially empty:
>>> num = [] >>> for i in range(3, 15, 2): num.append(i)We check the value of the variable to see if the items were appended successfully and confirm that the list is not empty anymore:
>>> num [3, 5, 7, 9, 11, 13]💡 Tip: We commonly use >>> num = [] >>> bool(num) False3 to add the first element to an empty list, but you can also add this element calling the >>> num = [] >>> bool(num) False4 method with index >>> num = [] >>> bool(num) False1:
🔸 Using the list() Constructor
Alternatively, you can create an empty list with the type constructor >>> num = [] >>> len(num) 09, which creates a new list object.
According to the :
If no argument is given, the constructor creates a new empty list, >>> num = [] >>> len(num) 08.
💡 Tip: This creates a new list object in memory and since we didn't pass any arguments to >>> num = [] >>> len(num) 09, an empty list will be created.
For example:
num = list()This empty list will have length >>> num = [] >>> bool(num) False1, as you can see right here:
>>> num = list() >>> len(num) 0And it is a falsy value when it is empty (it evaluates to >>> num = [] >>> bool(num) False2 in a boolean context):
>>> num = [] >>> len(num) 00Example:
This is a fully functional list, so we can add elements to it:
>>> num = [] >>> len(num) 01And the result will be a non-empty list, as you can see right here:
>>> num [3, 5, 7, 9, 11, 13]🔹 Use Cases
- We typically use >>> num = [] >>> len(num) 09 to create lists from existing iterables such as strings, dictionaries, or tuples.
- You will commonly see square brackets >>> num = [] >>> len(num) 08 being used to create empty lists in Python because this syntax is more concise and faster.
🔸 Efficiency
Wait! I just told you that >>> num = [] >>> len(num) 08 is faster than >>> num = [] >>> len(num) 09...
But how much faster?
Let's check their time efficiencies using the module.
To use this module in your Python program, you need to import it:
>>> num = [] >>> len(num) 03Specifically, we will use the from this module, which you can call with this syntax:
💡 Tip: The code is repeated several times to reduce time differences that may arise from external factors such as other processes that might be running at that particular moment. This makes the results more reliable for comparison purposes.
🚦 On your marks... get set... ready! Here is the code and output:
First, we import the module.
Then, we start testing each syntax.
Testing >>> num = [] >>> len(num) 08:
>>> num = [] >>> len(num) 05Testing >>> num = [] >>> len(num) 09:
>>> num = [] >>> len(num) 06💡 Tip: Notice that the code that you want to time has to be surrounded by single quotes This list is empty4 or double quotes This list is empty5. The time returned by the >>> num = [] >>> bool(num) False0 function is expressed in seconds.
Compare these results:
- >>> num = [] >>> len(num) 08: This list is empty8
- >>> num = [] >>> len(num) 09: >>> num = [] >>> for i in range(3, 15, 2): num.append(i)0
You can see that >>> num = [] >>> len(num) 08 is much faster than >>> num = [] >>> len(num) 09. There was a difference of approximately >>> num = [] >>> for i in range(3, 15, 2): num.append(i)3 seconds in this test:
>>> num = [] >>> len(num) 07I'm sure that you must be asking this right now: Why is >>> num = [] >>> len(num) 09 less efficient than >>> num = [] >>> len(num) 08 if they do exactly the same thing?
Well... >>> num = [] >>> len(num) 09 is slower because it requires looking up the name of the function, calling it, and then creating the list object in memory. In contrast, >>> num = [] >>> len(num) 08 is like a "shortcut" that doesn't require so many intermediate steps to create the list in memory.
This time difference will not affect the performance of your program very much but it's nice to know which one is more efficient and how they work behind the scenes.
🔹 In Summary
You can create an empty list using an empty pair of square brackets >>> num = [] >>> len(num) 08 or the type constructor >>> num = [] >>> len(num) 09, a built-in function that creates an empty list when no arguments are passed.
Square brackets >>> num = [] >>> len(num) 08 are commonly used in Python to create empty lists because it is faster and more concise.
I really hope that you liked my article and found it helpful. Now you can create empty lists in your Python projects. Check out my online courses. Follow me on Twitter. ⭐️
If you want to dive deeper into lists, you may like to read:
- Python List Append – How to Add an Element to an Array, Explained with Examples
- The Python Sort List Array Method – Ascending and Descending Explained with Examples
- Python List Append VS Python List Extend – The Difference Explained with Array Method Examples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Developer, technical writer, and content creator @freeCodeCamp. I run the freeCodeCamp.org Español YouTube channel.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started