A list in Python is one of the most used data types, loosely resembling an array in other programming languages. Lists are mutable; hence, they can be modified after it is created.
This article is for you if you want to learn how to create an empty list efficiently and add elements to it in any position.
How to create an empty List in Python
To create an empty list in Python, use the square brackets [ ] and assign them to a variable, which will become an empty list.
data = []
We can verify that the data list is empty by checking its length using the len() function. If it returns 0, the list is empty; otherwise, it is not.
data = [] print(len(data))
Output
0
You can see that the list is empty. However, there is another way to check if the list is empty.
Use the bool() method to check if the list is empty. Empty lists are falsy values, meaning they evaluate to False in a boolean context.
data = [] print(bool(data))
Output
False
Create an empty list using the list() constructor
Use the type constructor list() to create an empty list. The list() constructor creates a new list object. If no argument is given in the list constructor, the constructor creates a new empty list [ ].
data = list()
This empty list will have a length of 0.
data = list() print(len(data))
Output
0
As expected, it returns 0 length, meaning we have successfully created an empty list. And it is a false value when it is empty.
data = list() print(bool(data))
Output
False
Comparison of [ ] vs list()
To check which approach is faster to create an empty list, use the timeit module. To use the timeit module in your Python, you must import it into your program.
To import a module in Python, use the import statement at the program’s start.
import timeit
Now, write the following code to check the performance of both approaches.
import timeit # Testing [ ] print(timeit.timeit("[]", number=10**4)) # Testing list( ) print(timeit.timeit("list()", number=10**4))
The timeit() function takes two parameters.
- Code
- How many times to run that code
The less time it takes to create an empty list, the more efficient the code. Let’s run the above code and see the output.
0.00032584700000000064 0.0014368230000000003
From the output, you can see that “[ ]” takes less time and is more efficient than the list() constructor.
In conclusion, we can safely say that we always use square brackets to create an empty list.
But why list() constructor is less efficient than [ ] if they do the same thing? Let’s find out.
The list() is slower because it requires looking up the function’s name, calling it, and then creating the list object in memory. In addition, it goes through some middle steps, which take time.
The square brackets([ ]) are like a “shortcut” that doesn’t require so many middle steps to create the list in memory.
How to append elements to an empty list
To append elements in an empty Python list, use the append() or insert() method.
Python list append()
The list append() method adds the element to the end of the list. The append() method takes one argument, an element we need to add to the list.
lst = [] for i in range(1, 10, 2): lst.append(i) print(lst)
Output
[1, 3, 5, 7, 9]
In this example, we used for loop and range() to create a sequence and append sequence elements one by one to the list and print the list.
Python list insert()
The list insert() method adds the element at the particular index of your chosen list. The insert() function takes two parameters: The first is an index on which we need to add an element, and the second is the element itself.
lst = [] lst.insert(0, 11) print(lst)
Output
[11]
In this example, we pass the 0th index and 11 element, which returns a list with one element.
Bottom Line
Always use square brackets to define or create an empty list in Python. To check if the list is empty, use either len() function or bool() function to check if it returns falsy value.
That’s it.