I want to create an empty list and append some values inside this list. So far I know that if I want to create and empty list I can do it either like this:
list1 = []
or like this:
list1 = list()
(My apologies if there is another way that currently I don't know)
My question at this point is if there is a difference between those two ways to create a list; in terms of efficiency for example.
Thank you in advance for your help.
CodePudding user response:
I Don't Think it makes any difference, I can not see why you need to declare a new list using the constructor.
list1 = []
and list1 = list()
are the same as I know.
If you have literal values you should use the literal notation list1= ['a', 'b', 'c']
I don't even think it's possible to create a list with some values using the constructor.
The constructor main usage is when we want to convert a Map
, Set
or 'tuple' into a list
For example:
my_tuple = ('a', 'b', 'c')
my_list = list(my_tuple)
CodePudding user response:
The literal syntax, list1 = []
, is slightly faster. There's been a handful of threads on this worth reading: