2019/12/11 postscript: In the comment, you pointed out an error in time measurement. We are currently revalidating with the correct code.
TL;DR
--[]
notation is more advantageous in execution time
--Note that the handling of tuples is partially different between list ()
and []
.
Python has the philosophy There should be one--and preferably only one --obvious way to do it.
, but there are two ways to write a list, which is one of the most frequently used data structures. Is it against this philosophy?
Is there any difference that allows for different definitions? I was worried.
StackOverflow had a similar question in 2015.
One's a function call, and one's a literal:
Well, list ()
is a function and []
is a literal.
Well I do not know.
For the sake of completion, another thing to note is that list((a,b,c)) will return [a,b,c], whereas [(a,b,c)] will not unpack the tuple. This can be useful when you want to convert a tuple to a list. The reverse works too, tuple([a,b,c]) returns (a,b,c).
Indeed, the behavior for tuples is different. Does this difference in behavior show that the above function and literal are?
Quora also had a similar question in 2016.
The end result is the same whether you use [ ] or list() and same for { } vs. dict(). There is a difference in performance, though. [ ] and { } are more performant than the alternatives, list() and dict(), mainly because using list() and dict() involves the overhead of symbol lookup and function invocation.
In my opinion, using [ ] and { } is more pythonic. In addition, this notation allows you to take advantage of list/dictionary comprehension.
By the way, even in the dictionary type, there are two ways to define it with dict ()
and {}
.
Is the literal notation more Pythonic (like Python)? I still don't understand Python at all.
I found that some of them behave differently, but which one should be used in situations where they behave in the same way? Since there is a difference in the call between the function and the literal, I tried to verify it with the code that creates the object 100 times each.
list.py
#-*- using:utf-8 -*-
import time
if __name__ == '__main__':
start = time.time()
for i in range(0,100):
exec("list_%d = %s" % (i, []))
elapsed_time1 = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time1) + "[sec]")
start = time.time()
for j in range(100,200):
exec("list_%d = %s" % (j, list()))
elapsed_time2 = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time2) + "[sec]")
~~ I see, there is a significant difference in execution time!
By the way, the significance of the execution time in the literal []
was also recognized when the number of loops was set to 1000 and 10000. ~~
As mentioned above, if you take a tuple as an argument at the time of generation, each will behave differently, What happens to the behavior after creating an object?
python
>>> l1 = []
>>> l1.append((1,2,3))
>>> print(l1)
[(1, 2, 3)]
>>> l2 = list()
>>> l2.append((1,2,3))
>>> print(l2)
[(1, 2, 3)]
There seems to be no difference in behavior once it is created as a list object. I think this area is Python or object-oriented ... I don't understand object-oriented.
Q. Can it be done? Can not? A. Either notation can be executed.
Recommended Posts