There are various complex data structures in Python. They include Lists, Tuples, Sets, and Dictionaries. They each have a specific function and how they are represented. For example, a Tuple can be represented with brackets ()
or the keyword tuple
.
In this Answer, we will review how to represent the list data structure. There are two ways to implement the array data structure: the square bracket []
or the keyword list
.
[]
?The square bracket []
is used to create a list. For example, to create a list that contains numbers ranging from 1–5, the square bracket can be used.
num = [1, 2, 3, 4, 5]print(num)
Printing num would return the list [1, 2, 3, 4, 5]
.
list()
function?The list()
function in Python is used to create a list from an iterable object. For example, if there’s a string “plant,” the list()
function can be used to convert it to a list.
word = "plant"word_to_list = list(word)print(word_to_list)
The code above will return “plant” in a list format ['p', 'l', 'a', 'n', 't']
. The list
function can also be used to convert other data structures to a list. For example, a set containing random numbers can be converted into a list.
rand_numbers = {4, 3, 7, 9}list_rand_numbers = list(rand_numbers)print(list_rand_numbers)
The code above will return a list of list_rand_numbers
in any format because of the set.
After understanding the ways a list can be represented, let’s address the issue. Which one is faster? The answer is that the square bracket method is faster than the function.
[]
faster?The reason why []
is faster is because it is a syntactic construct, while list()
is a function call. Function calls in Python have some overhead, which makes them slower than syntactic constructs. Therefore, using []
to represent a list is more efficient than using the list()
function. Also, the list()
function is slower than the []
syntax for creating a list because it requires additional memory allocation.
To further analyze this, the timeit library can be used to see the number of seconds it takes for each method to perform.
First, we import the timeit
library and see how many seconds it takes to run.
import timeitprint(timeit.timeit('[]', number=10**7))print(timeit.timeit('list()', number=10**7))
After running this code, there is about a 0.3 seconds per 10 million calls difference from using the square bracket and the function. To test further, a list containing numbers from 1–10 would be added.
import timeitprint(timeit.timeit('[1,2,3,4,5,6,7,8,9,10]', number=10**7))print(timeit.timeit('list([1,2,3,4,5,6,7,8,9,10])', number=10**7))
The result of running the code above is that it takes three times as long to run the list()
function with the list containing numbers from 1–10 than using the square bracket to create the list. This is because the function has to allocate additional memory for the list while the square bracket already allocates memory for the list. Imagine using the list
method over a large range of numbers, it’s going to take a lot of time!
In conclusion, we have seen that there are two ways to represent a list in Python: using square brackets []
and using the list()
function. While both methods create a list, the square bracket method is faster and more efficient than using the list()
function. This is because []
is a syntactic construct, while list()
is a function call that requires additional memory allocation. When working with large lists, it is best to use the square bracket method for better performance.