Why use the list() constructor?
Question:
I just wanted to know the use of the list() constructor because if we want to create a new list then just we can use square brackets, what’s the specific use of the list() constructor.
newGrocerylist = list(("apple", "guava", "mango", "pineapple", "orange")) #Why use the list() constructor.
print(newGrocerylist)
#We can also do like this--
newGrocerylist = ["apple", "guava", "mango", "pineapple", "orange"]
print(newGrocerylist)
Why use the list constructor? Isn’t it a redundant thing to do–
newGrocerylist = list(("apple", "guava", "mango", "pineapple", "orange"))
Answers:
Perhaps we want to convert a Map
or Set
into a list
. We would pass it into the constructor.
mylist = list(myset)
In the initial statement you created a tuple (indicated by round brackets) and converted it with list()
. In the second example you directly created a list (with the square brackets).
They don’t behave the same in all contexts. For example, you can construct a list of dictionary keys, or create a list with one element (one dict).
>>> list({1:2})
[1]
>>> [{1:2}]
[{1: 2}]
Check out: [] and {} vs list() and dict(), which is better?
It looks like the constructor [] or {} for dicts is much faster than list() or dict().
If you want to create a literal new list with a bunch of new values then you’re right. There is no reason to use the list constructor, you should use the literal notation:
my_list = ['a', 'b', 'c']
In fact, it is impossible to create a new list with a bunch of values using the constructor, you can only use it to transform iterables into their list representation:
my_tuple = ('a', 'b', 'c') # literal notation to create a new tuple
my_list = list(my_tuple) # this is what you actually did in your first example
You can use the other iterable constructors like set
and dict
in a similar way. They are not used to create new objects, but transform existing ones into the type they describe.
list
can be passed around as a function object.
So as a toy example you could have a function that creates an arbitrary collection and you could pass in either list
or set
def make_collection_from_data(data, collection_maker):
return collection_maker(data)
data = [1, 2, 3, 4]
make_collection_from_data(data, set) # Returns a list
make_collection_from_data(data, list) # Returns a set
Primarily we use a Constructorlist() when there is a string.
vowel_string = 'aeiou'
print(list(vowel_string))
OUTPUT
['a','e','i','o','u']
To my understanding, list () constructor uses less memory (thus less execution time) than list(). Check the code below and inform me if I am missing something:)
from timeit import default_timer as timer
start = timer()
list1 = ["apple", "banana", "cherry"]
print(list1)
stop =timer()
print(stop-start)
start =timer()
thislist = list(("apple", "banana", "cherry"))
print (thislist)
stop =timer()
print(stop-start)
The results in seconds:
List [0.0003924000193364918] and
list () constructor[0.00016659998800605536]
I just wanted to know the use of the list() constructor because if we want to create a new list then just we can use square brackets, what’s the specific use of the list() constructor.
newGrocerylist = list(("apple", "guava", "mango", "pineapple", "orange")) #Why use the list() constructor.
print(newGrocerylist)
#We can also do like this--
newGrocerylist = ["apple", "guava", "mango", "pineapple", "orange"]
print(newGrocerylist)
Why use the list constructor? Isn’t it a redundant thing to do–
newGrocerylist = list(("apple", "guava", "mango", "pineapple", "orange"))
Perhaps we want to convert a Map
or Set
into a list
. We would pass it into the constructor.
mylist = list(myset)
In the initial statement you created a tuple (indicated by round brackets) and converted it with list()
. In the second example you directly created a list (with the square brackets).
They don’t behave the same in all contexts. For example, you can construct a list of dictionary keys, or create a list with one element (one dict).
>>> list({1:2})
[1]
>>> [{1:2}]
[{1: 2}]
Check out: [] and {} vs list() and dict(), which is better?
It looks like the constructor [] or {} for dicts is much faster than list() or dict().
If you want to create a literal new list with a bunch of new values then you’re right. There is no reason to use the list constructor, you should use the literal notation:
my_list = ['a', 'b', 'c']
In fact, it is impossible to create a new list with a bunch of values using the constructor, you can only use it to transform iterables into their list representation:
my_tuple = ('a', 'b', 'c') # literal notation to create a new tuple
my_list = list(my_tuple) # this is what you actually did in your first example
You can use the other iterable constructors like set
and dict
in a similar way. They are not used to create new objects, but transform existing ones into the type they describe.
list
can be passed around as a function object.
So as a toy example you could have a function that creates an arbitrary collection and you could pass in either list
or set
def make_collection_from_data(data, collection_maker):
return collection_maker(data)
data = [1, 2, 3, 4]
make_collection_from_data(data, set) # Returns a list
make_collection_from_data(data, list) # Returns a set
Primarily we use a Constructorlist() when there is a string.
vowel_string = 'aeiou'
print(list(vowel_string))
OUTPUT
['a','e','i','o','u']
To my understanding, list () constructor uses less memory (thus less execution time) than list(). Check the code below and inform me if I am missing something:)
from timeit import default_timer as timer
start = timer()
list1 = ["apple", "banana", "cherry"]
print(list1)
stop =timer()
print(stop-start)
start =timer()
thislist = list(("apple", "banana", "cherry"))
print (thislist)
stop =timer()
print(stop-start)
The results in seconds:
List [0.0003924000193364918] and
list () constructor[0.00016659998800605536]